PHP Type-Cast Confusion - php

I have the following code:
<?php
$val = 0;
$res = $val == 'true';
var_dump($res);
?>
I always was under impression that $res should be 'false' as in the above expression PHP would try to type cast $val to boolean type (where zero will be converted as false) and a string (non-empty string is true). But if I execute the code above output will be:
boolean true
Am I missing something? Thanks.

In PHP, all non-empty, non-numeric strings evaluate to zero, so 0 == 'true' is TRUE, but 0 === 'true' is FALSE. The string true has not been cast to a boolean value, but is being compared as a string to the zero. The zero is left as an int value, rather than cast as a boolean. So ultimately you get:
// string 'true' casts to int 0
0 == 0 // true
Try this:
echo intval('true');
// 0
echo intval('some arbitrary non-numeric string');
// 0
Review the PHP type comparisons table. In general, when doing boolean comparisons in PHP and types are not the same (int to string in this case), it is valuable to use strict comparisons.

Because $val is the first operator PHP converts the string true to an integer which becomes 0. As a result 0 ==0 and your result is true;

Try this
<?php
$val = 1;
$res = (bool)$val == 'true';
var_dump($res);
?>

Related

String as boolean in PHP [duplicate]

For the record, I know the solution is to use === instead of == .
I'm just wondering what the logic behind it is. How is it logical that 'hello' can equal TRUE?
$var = TRUE;
if($var == 'hello'){
echo 'match';
}
else{
echo 'no match';
}
The solution has been discussed, but I haven't seen any real explanation.
String value equals true
== compares just the values of the variables whereas === compares variable values and type. so for an example:
1 == 1: true
1 === "1": false // "1" is a string and 1 is an integer
when asking if a string == true, you are essientially asking if it is set. Similar functionality is behind the isset() method.
If you were to compare "hello" === true. This would be false as they are of different type and "hello" would HAVE to equal "hello"
When using == operator think in falsy and truthy terms.
So:
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integers 0 and -0 (zero)
the floats 0.0 and -0.0 (zero)
the empty string, and the string "0"
an array with zero elements
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
-1 is considered TRUE, like any other non-zero (whether negative or positive) number!
Every other value is considered TRUE (including any resource and NAN).
#see: https://www.php.net/manual/en/language.types.boolean.php

my List of comparison operators in php returns '1' when I use keyword echo why

I'm begenning at PHP , I was poking around my code and learning about List of comparison operators , however I'll try out to put echo before my comparasion operators and I've received this result : 1 then I though the reason is comparasion true equal to 1, else equal to 0 , in this moment seemed me somethig kinda python, yet I just got the 1 . why not 0 as result ?
Is attached my question
You're printing a boolean value. The value is converted to a string where 1 represents true and a blank string represents false.
From the manual:
A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.
http://php.net/manual/en/language.types.string.php
You could use the ternary operator to output the string true or false where appropriate. Example:
echo (10 >= 12) ? 'true' : 'false';
when print boolean value in php it will print 1 if TRUE and "" if FALSE.
if you want to print 0 if FALSE then you can convert into int.
in your case you can use like this
$bool = 10 >= 12;
echo (int)$bool;
it will return 0.
<?php
echo 10>=9 // print 1 as it is true
echo 11>3 // print 1
echo 11 == 11 // print 1
echo 10>=12 // print nothing because it is false
This happening becuase a boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string).

How is it logical that a string equals TRUE in PHP

For the record, I know the solution is to use === instead of == .
I'm just wondering what the logic behind it is. How is it logical that 'hello' can equal TRUE?
$var = TRUE;
if($var == 'hello'){
echo 'match';
}
else{
echo 'no match';
}
The solution has been discussed, but I haven't seen any real explanation.
String value equals true
== compares just the values of the variables whereas === compares variable values and type. so for an example:
1 == 1: true
1 === "1": false // "1" is a string and 1 is an integer
when asking if a string == true, you are essientially asking if it is set. Similar functionality is behind the isset() method.
If you were to compare "hello" === true. This would be false as they are of different type and "hello" would HAVE to equal "hello"
When using == operator think in falsy and truthy terms.
So:
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integers 0 and -0 (zero)
the floats 0.0 and -0.0 (zero)
the empty string, and the string "0"
an array with zero elements
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
-1 is considered TRUE, like any other non-zero (whether negative or positive) number!
Every other value is considered TRUE (including any resource and NAN).
#see: https://www.php.net/manual/en/language.types.boolean.php

php variables comparison == vs ===

this is code:
$s = 0;
$d = "dd";
if ($s == $d) {
var_dump($s);
die(var_dump($d));
}
result is:
int 0
string 'dd' (length=2)
Please explain why.
why ($s == $d) results as true?
Of course, if === is used it will results as false but why this situation requires ===?
Shouldn't it be returned false in both situations?
Because (int)$d equals with 0 and 0=0
you must use strict comparison === for different character tyes (string) with (int)
Your $d is automatically converted to (int) to have something to compare.
When you compare a number to a string, the string is first type juggled into a number. In this case, dd ends up being juggled into 0 which means that it equates to true (0==0).
When you change the code to:
<?php
$s = 1;
$d = "dd";
if ($s == $d)
{
var_dump($s);
die(var_dump($d));
}
?>
You will find that it doesn't pass the if statement at all.
You can more details by reading up on comparison operators and type juggling.
The string "dd" is converted to int, and thus 0.
Another example :
if ( "3kids" == 3 )
{
return true;
}
And yes, this returns true because "3kids" is converted to 3.
=== does NOT auto convert the items to the same type.
Also : 0 == false is correct, but 0 === false is not.
See : http://php.net/manual/en/language.types.type-juggling.php
The string will try to parsed into a number, returns 0 if it is not in right number format.
As seen in the php website :
http://php.net/manual/en/language.operators.comparison.php
var_dump(0 == "a"); // 0 == 0 -> true
In PHP, == should be pronounce "Probably Equals".
When comparing with ==, PHP will juggle the file-types to try and find a match.
A string with no numbers in it, is evaluated to 0 when evaluated as an int.
Therefore they're equals.

Difference between numeric and string value in PHP

I am getting a difference when comparing two string which are 0 and '0' in PHP. Can I do anything to make them be compared equally in an if action?
Thanks!
If you compare using:
if ('0' == 0) // if '0' is equal to 0
it should return true as the values are compared with the string being converted to a number. If you do:
if ('0' === 0) // if '0' is identical to 0
it will return false as they have to be of the same type too.
Note the triple '='
You can also force their type to be the same before comparing:
if((int)'0' === (int)0) {
// true
}
if((string)'0' === (string)0) {
// true
}

Categories