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

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

 

Last updated 4 years, 10 months ago | 1758 views 75     5

Tags:- PHP

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 the 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 the same so it will return true.

var_dump('0123' === 123) 

This will outputs bool(false) since "===" is an identical operator that 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.