When developing a website it helpful to have PHP display the errors. By default display_errors is set to false. While in development, it is helpful to turn this on. Just be sure to turn it back off after the web page complete and working properly. For a number of reasons, including security, users of the website should not have access to the error messages.There are several ways this can be accomplished depending on the desired scope.
Individual web page:
At the top of the web page add the following code:
<?php
ini_set ('display_errors','On');
error_reporting(E_ALL);
?>Entire web server:
Locate the php.ini file
- Mac: /Applications/MAMP/bin/php/php5.3.6/conf/php.ini (Note:php5.3.6 might be a different version)
- Windows: Using the WAMP menu in the task bar, go to PHP then PHP.ini
Locate the following lines and make the change to on a on and E_ALL
- In the PHP.ini file it states
; Print out errors (as a part of the output). For production web sites,
; you're strongly encouraged to turn this feature off, and use error logging
; instead (see below). Keeping display_errors enabled on a production web site
; may reveal security information to end users, such as file paths on your Web
; server, your database schema or other information.
display_errors = off- However, if you need assistance with debugging your code change this setting to
display_errors = on
- Just above that paragraph make sure the error_reporting is set as follows
error_reporting(E_ALL);
Level of Error Reporting Set on Each page
Configure PHP.ini to display_errors=on
At the top of each page, set the level of error/message reporting with the following code:
<?php
error_reporting(E_ALL);
?><?php
error_reporting(E_ERROR | E_PARSE); //Fatal and parse errors
?><?php
error_reporting(E_ERROR &~ E_NOTICE); //All but run-time notices
?>Debugging Technique:
To view the PHP code in the browser window with the code highlighted, use the following to any file in the same directory.
<?php
highlight_file("test.php");
?>