What will be the output of the following statements and why?

38 Views

What will be the output of the following statements and why?

PHP | Output of the following statements var_dump(0123 == 123); var_dump('0123' == 123); var_dump('0123' === 123

 

var_dump(0123 == 123)

This will output bool(false) because the leading 0 in 0123 tells PHP interpreter to treat the value as octal rather than decimal, so the value of 0123 in decimal is equal to 83. so it is clear that 83 is not equal to 123 and will return false.

var_dump('0123' == 123) 

This will output bool(true) since the string '0123' will automatically be converted to integer 123 and the leading 0 is ignored. Now both are same so it will return true.

var_dump('0123' === 123) 

This will outputs bool(false) since "===" is an identical operator which checks value as well as data type, so it performs a more strict comparison and does not do the automatic type conversion of string to an integer. Hence will return false.

Tags:-
PHP