“0″ is not Empty

PHP offers a nifty thing called “silent type conversion” which allows data of different types to be easily compared with data of different types. This is cool because I can say that the integer 1 and the boolean true can be evaluated to be the same thing. This becomes interesting when the concept of “emptiness” comes into play.

In my head, it makes perfect sense to say that a string zero characters in length and an array with no values are both empty. Likewise, the integer 0 can also be evaluated as an empty value too. That makes total sense.

<?php $emptyString = ''; $emptyArray = array(); if(empty($emptyString) && empty($emptyArray)) {     echo 'empty'; } ?>

What doesn’t make sense is that PHP considers a 1 character long string with a value of “0″ to be empty.

<?php $zeroString = '0'; $emptyArray = array(); if(empty($zeroString)) {     echo 'empty'; } ?>

I’m sorry — that’s complete bullshit.

After witnessing a philosophical holy war unfold on the internals list over PHP 6’s namespace operator, I’m surprised that something this ridiculous made it into userland.

This entry was posted on Friday, August 24th, 2007 at 3:26 pm and is filed under PHP. Both comments and pings are currently closed.

Comments are closed. Sorry.

Site Stuff

Pages

Projects

Archives

Categories