Skip to content

Categories:

Undefined variables == Undefined behaviour.

fboender

Here's a nice one for a PHP exam. Given this complete code example, will it print 'aa' or 'bb'?

<?php

if ($variable != 0) {
  print('aa');
} else {
  print('bb');
}

?>

Let's apply the rules of logic here. $variable is unset, so it is not '0'. Therefor it should print 'aa'.

Wrong.

Let's apply the rules of PHP here. PHP will BLACK BOX MAGIC VOODOO and from that it naturally follows that PHP will print 'bb':

[todsah@host]~$ php ./magic_voodoo.php

Notice: Undefined variable: variable in /home/todsah/magic_voodoo.php on line 3
bb

And that, dear PHP developers, is why warnings and notices in PHP should be removed in favour of errors. At. All. Times. I hate PHP.

AFMELDING

Posted in Software Bashing.

Tagged with , , , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

You must be logged in to post a comment.