The Linux Page

PHP Variables and E_NOTICE

Today I learned that not defining a variable in PHP would generate a notice error.

I had been wondering, why it is that so many people in Drupal define their variable or check them with isset()? That's why. In a way it is terrible that you can use a variable that does not exist and just get NULL, on the other hand, quite often that's because you intended to use another variable... (i.e. $create instead of $created, you declare $bool in one function and use it again in another, etc.)

This applies to objects, array indexes, and just plain variables. If you write:

  echo $foo;

and you never had $foo = 'something'; then you'll get a notice.

And note that:

  $foo = NULL;
  echo $foo;

does not generate any error! Because in that case the $foo variable exists, even though it is set to NULL.