Tuesday, June 29, 2010

division by zero is barely legal, but so false!

php > $a = 2/0;
PHP Warning: Division by zero in php shell code on line 1
php > var_dump( $a );
bool(false)

Friday, June 11, 2010

Random encounter: empty and indexing strings

php > $a = "I'm a string";
php > var_dump( empty($a['i am an index']) );
bool(false)

php > $a = "0I'm a string";
php > var_dump( empty($a['i am an index']) );
bool(true)

php > $a = " I'm a string";
php > var_dump( empty($a['i am an index']) );
bool(false)

And another inspired result:


$str = "a 0-gravity cat";
var_dump(empty($str['does not exist'])); // false
var_dump(empty($str['2 bottles of beer ...'])); // true
var_dump(empty($str['0x2 is numbericly!'])); // false
var_dump(empty($str['02 is numbericly!'])); // true
var_dump($str['010 numbericly!']); // y
var_dump("0x2" == 2); // true
var_dump("0x2" == "2"); // famously true

And just another finding investigating the issue even more:

php > var_dump( (int)'0x2' );
int(0)
php > var_dump( (int)'0x2' );
int(0)
php > var_dump( floor('0x2') );
float(2)
php > var_dump( (float)'0x2' );
float(0)

What else do I have to write?