LEARN TO CODE

View Original

How to Turn on PHP Error Reporting in MAMP

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:

See this content in the original post

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:

See this content in the original post

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.

See this content in the original post

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.

See this content in the original post

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

See this content in the original post

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