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'; } 
  • five
    And why should d be displayed? The numeric data type int can in no way be identical to a logical (boolean) type (true). - Mike
  • Well, see (bool) - it is a logical type, and therefore it displays "e". "d" does not print because we compare $ x as an integer type variable with true - Hardc0re
  • one
    aaaaaaaa ... got it! Well, thank you guys! - Beginner

1 answer 1

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