What will be the output of each of the statements below and why?

Started by jaintech, 01-06-2016, 02:26:30

Previous topic - Next topic

jaintechTopic starter

var_dump(0123 == 123);
var_dump('0123' == 123);
var_dump('0123' === 123);


anilkh7058

The output of the statements will be as follows:

1. var_dump(0123 == 123);
   Output: bool(true)
   Explanation: The leading 0 in 0123 indicates an octal number, so 0123 is equivalent to the decimal value 83. Therefore, 0123 is equal to 123 and the comparison returns true.

2. var_dump('0123' == 123);
   Output: bool(true)
   Explanation: The string '0123' is automatically converted to the integer 123 for the comparison, so the comparison returns true.

3. var_dump('0123' === 123);
   Output: bool(false)
   Explanation: The strict comparison operator (===) checks both the value and the data type. Since '0123' is a string and 123 is an integer, the comparison returns false.

In summary, the first two comparisons return true because the values are considered equal after automatic type conversion, while the third comparison returns false because the strict comparison checks both the value and the data type.
:)
  •