How to display PHP errors on your LAMP/WAMP setup
Posted May 21, 2010 at 8:00 pm in GeneralIs there anything more annoying than not being able to determine why your code isn’t working? How about trying to determine a reason why without receiving any feedback such as error messages? This is what happened to a friend of mine who is attempting to learn how to program using PHP and wasn’t able to tell why his code wasn’t working properly. Luckily, the solution is quite trivial.
You’ll need to modify PHP’s initialization file called php.ini. This file is responsible for configuring many of the aspects of PHP’s behavior. I will assume for the sake of brevity that you have a working LAMP/WAMP setup.
Let’s begin to edit the php.ini configuration file by running the following command:
sudo nano /etc/php5/apache2/php.ini
After you’ve opened the file and have taken a look at it you’ll want to find the section pertaining to error handling and logging. It might be a good idea to read this section if you want to change any of your predefined directives. If you simply want error messages to be displayed to your stdout then you’ll want to find the display_errors directive.
Below is the default setting (line without a preceding semicolon) for this directive and came from my php.ini file. READ CAREFULLY:
; This directive controls whether or not and where PHP will output
; errors, notices and warnings too. Error output is very useful during
; development, but it could be very dangerous in production
; environments. Depending on the code which is triggering the error,
; sensitive information could potentially leak out of your application
; such as database usernames and passwords or worse. It's
; recommended that errors be logged on production servers
; rather than having the errors sent to STDOUT.
; Possible Values:
; Off = Do not display any errors
; stderr = Display errors to STDERR (affects only CGI/CLI binaries!)
; On or stdout = Display errors to STDOUT
; Default Value: On
; Development Value: On
; Production Value: Off
; http://php.net/display-errors
display_errors = Off
You will want to change the last line in the block above to read:
display_errors = On
Save your php.ini file. You may or may not have to restart your Apache server using this command:
sudo /etc/init.d/apache2 restart
You should now be able to read error messages produced by your code!
References:
1. http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors
Commentary