Friday, November 20, 2009

Identify yourself!

<?
$a = array(1,2,3);
$b = array(1,2,3);

var_dump($a === $b); // true;

$a[] = 4;

var_dump($a === $b); // false

Result:
bool(true)
bool(false)


In other words: You can't check if you have two variables that point to an array are in fact pointing to different arrays or if they are pointing to the same array.

Thursday, November 19, 2009

switch your case

<?
switch(0) {
case "text": var_dump("casejump"); break;
default: var_dump("default");
}


Output is:
casejump


This is so because 0 == "string" is always true because it converts the string to a number, which results in 0.

Tuesday, November 17, 2009

Java casting mayhem

Wanna cast a Double object to an Integer object? Not sure, but seems like this is the required casting:
(Integer)(int)(double)(Double)somedoubleobject

The (Integer) might be left away however. Well, most often.

foo()[0] doesn't work

<?

function foo () {
return new array(foo);
}

$arr = foo();
$arr[0](); // works
var_dump(foo()[0]); // compiling error

Doesn't work. Sucks.

Monday, November 16, 2009

'0x00' == '0'

<?
var_dump('0x00' == '0');
var_dump('0x0a' == '10');

will print out:


bool(true)
bool(true)

To emphasize: This is a string-string comparison. And the strings are anything but equal.

Friday, November 13, 2009

And != &&

<?

$a = 1;
$b = 0;

$c = $a and $b;
$d = $a && $b;

echo "'$c' != '$d' \n";


Outcome:

'1' != ''


Documented behavior. See http://www.php.net/manual/en/language.operators.precedence.php