2007.05.01

PHP Idiosyncrasies

Three reasons to make sure you know your programming language:


<?php

function test($code) {
$result = @eval("return ({$code});");
print ($result ? 'true ' : 'false ') . "$coden";
}

test( "gettype(key(array('s' => ''))) == 'string'" );
test( "gettype(key(array('1' => ''))) == 'string'" );

test( "'s' == 0" );
test( "0 == 's'" );

test( "$x == $x++" );
test( "(int)$x == $x++" );

And here's the output:


true gettype(key(array('s' => ''))) == 'string'
false gettype(key(array('1' => ''))) == 'string'
true 's' == 0
true 0 == 's'
false $x == $x++
true (int)$x == $x++

In case you didn't catch what PHP is doing on the last pair, here's the equivalent processing in PHP code:


$y = &$x;
$x++;
($x == $y)

When casting $x with (int), PHP is no longer able to make use of the "let's just use a reference" over-optimization. Personally, I think it's a bug that the behaviour differs between the two.


Debuggers beware! ;)

18:50 Posted in General | Permalink | Comments (0) | Email this

The comments are closed.