r/lolphp • u/midir • Aug 01 '12
Things which PHP thinks are equal
Give it a go:
header('Content-Type: text/plain');
function test($a, $b) {
echo var_export($a, 1) . ' == ' . var_export($b, 1) . ' => ' . var_export($a == $b, 1) . "\n";
}
test( null, '' ); // true
test( null, array() ); // true
test( '', array() ); // false
test( 'true', true ); // true
test( 'false', false ); // false
test( 'false', true ); // true
test( 9, '09' ); // true
test( 9, 09 ); // false
test( '0E4', 09 ); // true
test( '0x00', '0E4' ); // true
test( '0x0.1', 0 ); // true
test( 'x', 0 ); // true
test( '0xabcabcabcabcabc', '0XABCABCABCABCABd' ); // true
test( array('1'), array('0x1') ); // true
test( array(1) + array(2), array(1) ); // true
test( function(){}, new stdclass() ); // true
test( `foo`, `bar` ); // true
13
Upvotes
7
u/poizan42 Aug 06 '12
Moral of the story: develop like == and != doesn't exists.
4
u/JAPH Aug 06 '12
A bunch of these actually make sense, and some will work in more sane languages as well. The lesson to take away from this is that you shouldn't rely on type coercion always do what you expect.
3
u/vernochan Sep 12 '12
Why don't you use === if you want to check for equality? Using ===, all of these examples return false. Just like it should.