php.ini and Undefined Variable errors

It all started with a new server. After setting up and installation everything was working fine, until I read the error_log files in the servers logs.

There were loads and loads of error messages reporting that there were “undefined variables” throughout every script on the server. Strange!

It ended up being very easy to fix…

The errors displayed:

[Thu Feb 19 14:35:37 2009] [error] [client xx.xx.94.2] PHP Notice:  Undefined variable: page2 in /xxx/xxx/xxx/0100.tv/httpdocs/xxx/xxx.php on line 3
[Thu Feb 19 14:35:37 2009] [error] [client xx.xx.94.2] PHP Notice:  Undefined variable: page3 in /xxx/xxx/xxx/0100.tv/httpdocs/xxx/xxx.php on line 4
[Thu Feb 19 14:35:37 2009] [error] [client xx.xx.94.2] PHP Notice:  Undefined variable: page4 in /xxx/xxx/xxx/0100.tv/httpdocs/xxx/xxx.php on line 5
[Thu Feb 19 14:35:37 2009] [error] [client xx.xx.94.2] PHP Notice:  Undefined variable: page5 in /xxx/xxx/xxx/0100.tv/httpdocs/xxx/xxx.php on line 6
[Thu Feb 19 14:35:37 2009] [error] [client xx.xx.94.2] PHP Notice:  Undefined variable: page6 in /xxx/xxx/xxx/0100.tv/httpdocs/xxx/xxx.php on line 8

This was awfully confusing considering that every variable was working. However, it comes down to sloppy coding/PHP’s error reporting settings. You should define every variable by typing var $variable; before using it. I’m new to coding and this is a lesson learnt, however a lot of scripts use this sloppy technique so instead….

Reading through the error reporting description at the top of php.ini we read:

By default, PHP surpresses errors of type E_NOTICE.  These error messages
    are emitted for non-critical errors, but that could be a symptom of a bigger
    problem.  Most notably, this will cause error messages about the use
    of uninitialized variables to be displayed.

To stop there errors you can change error reporting php.ini by doing the following:

change

error_reporting  =  E_ALL

to

error_reporting = E_ALL & ~E_NOTICE

This will then only show the relevant error messages and supresses non-critical errors. Sorted.

One thought on “php.ini and Undefined Variable errors”

Comments are closed.