PHP is Not as Untyped as You May Believe
I stumbled upon an ugly PHP bug today and thought that I would share. While PHP is supposed to be a untyped language, this isn't always the case. The following code snippet for example does not do what you might expect:
switch ($output->status) { case 0: $output->status = 'fail'; break; case 1: $output->status = 'ok'; break; case 2: $output->status = 'stub'; break; }
With this code, passing in a string such as "ok", $output->status is set to "fail". This is due to what I assume to be a bug in PHP's lack of keeping everything untyped. For some reason, it would seem that PHP parses $output->status as an integer (therefore all strings return as 0) and then compares them to the list. If however you change the cases to strings:
switch ($output->status) { case '0': $output->status = 'fail'; break; case '1': $output->status = 'ok'; break; case '2': $output->status = 'stub'; break; }
Everything works as expected. Pretty lame if you ask me, but there it is.
Comments
Taavi
9 Jan 2009, 9:20 p.m. |
Nice.
I've lost all faith in PHP the language and the developer community around it. Nothing about it surprises me any more. Which isn't to say it doesn't get my goat whenever I waste time trying to figure out what's just happened...
Daniel
10 Jan 2009, 9:18 a.m. |
Taavi! It's always nice to know that there are nerds reading this site, even nicer to hear that they're friends :-)
So you're not a fan of PHP eh? What're your feelings regarding Python & Django? I find myself loving them more and more lately. Wait, doesn't Freshbooks do a lot of PHP? How are things over there anyway?
Post a Comment of Your Own
Preview