Before digging further into the PHP language, it's useful to configure your server's error reporting settings so that you're provided feedback when there are problems in your code.
PHP's error reporting threshold and display method can be configured via your server's PHP configuration file,
Because there are often multiple
php.ini files on a system, we want to make sure we edit the one that our local server is actually using. To determine this, create a new file in your document root called
info.php and paste in the following code:
<?php phpinfo(); ?>
When run, this built-in
phpinfo function will display all the PHP configuration information your server is using.
In this table of info, we're interested in the following 3 values:
- The location of your
php.inifile as indicated by the value for Loaded Configuration File
- MAMP on Mac:
/Applications/MAMP/bin/php/php7.2.x/conf/php.ini(The specific version number may vary, but it should be something >= 7.2)
- XAMPP on PC:
- What your
display_errorsconfiguration is set to. By default, it's likely
offand we'll change it to
- What your
error_reportingconfiguration is set to (it'll be a numerical value)
Update your settings
To adjust these settings, open the
php.ini file from the path found in the above steps, and set...
To make your changes take effect, restart your server.
Confirm the changes by refreshing your
info.php page and confirming
32767. (This value, 32767, is the numerical equivalent of E_ALL)
These two settings will make it so that all levels of PHP errors will be displayed on the page when they occur.
On our production servers we'll configure it so that errors are only written to log files (to avoid accidentally displaying errors to our visitors.)
With errors now accessible, let's learn about the different error types you may see...
The following are the most common error types you'll encounter when working with PHP:
Fatal run-time errors that will prevent the execution of your script from completing execution.
Example, try to import a file that does not exist:
Fatal error: require(): Failed opening required 'this-file-is-bogus.php' (include_path='.:/Applications/MAMP/bin/php/php7.1.0/lib/php') in /Applications/MAMP/htdocs/hello-world/errorExamples.php on line 3
Fatal compile errors that will prevent your script from executing.
Example, missing semi-colon:
echo "Hello" echo "World"
Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in /Applications/MAMP/htdocs/errorExamples.php on line 4
A warning is a non-fatal error that means something went wrong, but it will not stop the execution of your script.
Example, improper use of a built-in function:
Warning: rand() expects exactly 2 parameters, 1 given in /Applications/MAMP/htdocs/hello-world/errorExamples.php on line 3
A notice indicates you're doing something that is not exactly an error, but could be problematic. Notices will not stop the execution of your script.
Example, trying to use a variable that has not yet been defined:
Notice: Undefined variable: foo in /Applications/MAMP/htdocs/hello-world/errorExamples.php on line 3
While notices won't prevent your scripts from running, they should always be resolved as they are usually an indication of poorly written code.