How to Turn on PHP Error Reporting in MAMP

phplogo.jpg

One of my students asked a question this week about error reporting. When following one of the lessons in the PHP for Beginners course he should have received an error (intended error) but it did not show. He was curious as to why he did not receive the error as he was supposed to.

If you have not set error reporting up within your php.ini file then you will need to edit the file to do this. However, for a beginner, this can be quite daunting. To avoid any unnecessary issues with possible incorrect edits of this important file, you can turn on error reporting within a single script as follows:

Line 3: we are just saying that we want all types of errors to be reported

Line 4: this turns on error reporting (the value 1 is on and 0 would be off)

For those who wish to delve into their php.ini file then you will need to look for something along the lines of:

display_errors = on

error_reporting = E_ALL

I am currently using a Mac environment and have MAMP installed. MAMP actually creates several different php.ini files depending on the version of PHP you are using.

You will find the php.ini files in the following location on MAMP:

Applications > MAMP > conf

If you are running a Windows system with XAMPP then you should find it here:

C:/xampp/php/php.ini

If you are not sure where your php.ini file is located then you can write a simple script in PHP and run it in your browser to see the location of the php.ini file:

This will display the following:

Click to enlarge

I like to use a configuration script so that I can turn on/off error reporting depending on whether I am debugging or not.

Line 2: you can set the debug mode to true or false as required

Line 3: this will report all errors

Line 5: test if the debug mode is set to true

Line 6: turn on error display

Line 7: turn off error logging. It does not make much sense to write the error to a log file if you are reporting to the screen

Line 9: turn off display errors

Line 10: turn on the writing of errors to the log file

TYPES OF ERROR REPORTING

Within your php.ini file or within the script above you can specify the level of error reporting you require.

E_ALL             - All errors and warnings (includes E_STRICT as of PHP 5.4.0)
E_ERROR           - fatal run-time errors
E_RECOVERABLE_ERROR  - almost fatal run-time errors
E_WARNING         - run-time warnings (non-fatal errors)
E_PARSE           - compile-time parse errors
E_NOTICE          - run-time notices (these are warnings which often result from a bug in your code, but it's possible that it was intentional (e.g., using an uninitialized variable and relying on the fact it is automatically initialized to an empty string)
E_STRICT          - run-time notices, enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code
E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's initial startup
E_COMPILE_ERROR   - fatal compile-time errors
E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
E_USER_ERROR      - user-generated error message
E_USER_WARNING    - user-generated warning message
E_USER_NOTICE     - user-generated notice message
E_DEPRECATED      - warn about code that will not work in future versions of PHP
E_USER_DEPRECATED - user-generated deprecation warnings

Common settings within a php.ini file or the PHP script above include:

E_ALL (Show all errors, warnings and notices including coding standards.)
E_ALL & ~E_NOTICE  (Show all errors, except for notices)
E_ALL & ~E_NOTICE & ~E_STRICT  (Show all errors, except for notices and coding standards warnings.)
E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors)

Did you find this lesson useful? You could develop your coding skills further through one of the Coding.Academy courses.