php - Comparison of 0 to a string fails -
update: 0 isn't using default value, after testing condition failing suggested answers.
function test($value='a') { if ($value != 'a') { echo 'ok'; } else { echo 'not ok'; } } test(); // outputs not ok test('a'); // outputs not ok test(0); // outputs not ok, should output ok? test('0'); // outputs ok test(null); // outputs ok test(false); // outputs ok
this kind of throwing 1 of functions. surprised see 0 , null works fine. know why php interpreting 0 default value?
in php, comparing string 'a'
number 0
causes change of string number. in case 'a'
gets converted 0
, of course 0 == 0
.
see here details.
for completeness, fix, same linked documentation is
use strict comparison operators (===, !==) comparison operators
Comments
Post a Comment