Why when executing this script as a result, "e" is also displayed, but "d" is not displayed
$x = 1; if ($x == '1') { echo 'a'; } if ($x == true) { echo 'b'; } if((bool)$x === true){ echo 'e'; } if((int)$x === true){ echo 'd'; } Why when executing this script as a result, "e" is also displayed, but "d" is not displayed
$x = 1; if ($x == '1') { echo 'a'; } if ($x == true) { echo 'b'; } if((bool)$x === true){ echo 'e'; } if((int)$x === true){ echo 'd'; } Because the operation === compares not only the value of the operands, but also their type.
If $x=1 , then (bool)$x cast to bool type and true
(bool)$x === true - this comparison will give true , since both the value and the type match
(int)$x === true - here $x reduced to an integer ( int ) and compared to the bool type. Accordingly, although by value they both are considered true , but they still have a different type, so the comparison returns false
Source: https://ru.stackoverflow.com/questions/574992/
All Articles