About Error Handling and php.ini - Images: Japanese
Hey there, everyone. Hope you're doing well!
Let's continue on with error handling in PHP.
Throughout the previous articles, we've intentionally written broken PHP code to see what happens. By default, when you run PHP code that doesn't work, the page just goes completely blank.
A blank page tells you absolutely nothing about where the error is — which makes development pretty painful. Fortunately, with a small configuration change, PHP will start telling you exactly what went wrong.
Let's tweak the PHP settings a bit. PHP's global settings are controlled by a text file called 'php.ini'. Whether you installed PHP directly on a server, or you're running it through MAMP or XAMPP, the file is always called php.ini.
The location of php.ini can vary slightly depending on the package you installed and your OS, so let's start by finding it.
Also, if your OS is set to hide file extensions, please turn that off. Seeing file extensions is essential for development. If you're not sure how to do that — or want a quick overview of what extensions are — this article can help.
I honestly still don't understand why so many OSes hide file extensions by default...
To find php.ini, you could search online for "php.ini location" — but there's actually a built-in way to check without leaving your browser. Meet 'phpinfo()'.
<?php phpinfo();
Running this produces a page that looks something like this:

phpinfo() outputs a detailed summary of your PHP installation and its current settings.
Somewhere in the middle of that output, you'll see an entry called 'Loaded Configuration File'. The path shown to the right of that label is where your php.ini lives. The page also shows your PHP version, your web server software settings, and a lot more — so whenever you need to check your PHP environment, phpinfo() is your go-to.
Now that we know where php.ini is, let's make the change. php.ini is just a plain text file, so open it in any text editor you like.
Look for a line that says display_errors = Off — it's usually somewhere in the middle of the file. This controls whether PHP outputs error messages, so change it to display_errors = On.
There's one more setting to check. A little above display_errors, look for error_reporting = E_ALL. It's likely already set to E_ALL by default, but if it's set to something else, go ahead and change it to error_reporting = E_ALL. This setting controls which severity levels of errors PHP reports, and E_ALL tells it to catch pretty much everything.
You can also change the error reporting level from within a PHP file itself using 'error_reporting()', without touching php.ini. Just add the following line at the top of your PHP file:
<?php error_reporting(E_ALL);
This is handy when you want to temporarily raise or lower the error reporting level for a specific file.
One important note: error_reporting() only works if php.ini already has display_errors = On. If error_reporting() doesn't seem to be doing anything, go back and check that setting first.
Once you've saved your changes to php.ini, restart MAMP. Changes to php.ini don't take effect until the PHP runtime (in this case, MAMP) is restarted — so don't skip that step.
Let's give it a try. Here's a broken "Hello world" program:
<?php echo 'Hello world'
Yep, the semicolon is missing. Running this now produces:
Parse error: syntax error, unexpected end of file, expecting ',' or ';' in /Users/xxxx/Desktop/test/index.php on line 3
This message means "looks like you forgot a ',' or ';' somewhere around line 3." It's in English, but once you have error output enabled, PHP will point you to the suspicious part of your code like this — so simple mistakes like a missing semicolon won't leave you scratching your head for long. Development just got a lot easier.
And that covers PHP error handling and php.ini configuration.
Now that the development environment is set up properly, the next article moves on to one of the most fundamental concepts in any programming language: variables.
Keep it up — see you in the next one!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.