Skip to content

How to deal with errors in PHP

Every programmer makes errors. We’re humans, after all.

We might forget a semicolon. Or use the wrong variable name. Or pass the wrong argument to a function.

In PHP we have:

  • Warnings
  • Notices
  • Errors

The first 2 are minor errors, and they do not stop the program execution. PHP will print a message, and that’s it.

Errors terminate the execution of the program, and will print a message telling you why.

There are many different kinds of errors, like parse errors, runtime fatal errors, startup fatal errors, and more.

They’re all errors.

I said “PHP will print a message”, but.. where?

This depends on your configuration.

In development mode it’s common to log PHP errors directly into the Web page, but also in an error log.

You want to see those errors as early as possible, so you can fix them.

In production on the other hand you don’t want to show them in the Web page, but you still want to know about them.

What you do is, you log them to the error log.

This is all decided in the PHP configuration.

We haven’t talked about this yet, but there’s a file in your server configuration that decides a lot of things about how PHP runs.

It’s called php.ini.

The exact location of this file depends on your setup.

To find out where is yours, the easiest way is to add this to a PHP file and run it in your browser:

<?php
phpinfo();
?>

You will then see the location under “Loaded Configuration File”:

In my case it’s /Applications/MAMP/bin/php/php8.1.0/conf/php.ini.

NOTE: the information generated by phpinfo() contains a lot of other useful information, remember that.

Using MAMP you can open the MAMP application folder and open bin/php, go in your specific PHP version (8.1.0 in my case) then go in conf. In there you’ll find the php.ini file:

Open that file in an editor.

That contains a really long list of settings, with a great inline documentation for each one.

We’re particularly interested in display_errors:

In production you want its value to be Off, as the docs above it say.

The errors will not show up anymore in the website, but you will see them in the php_error.log file in the logs folder of MAMP in this case:

This file will be in a different folder depending on your setup.

You set this location in.. your php.ini:

The error log will contain all the error messages your application generates:

You can add information to the error log by using the error_log() function:

error_log('test');

It’s common to use a logger service for errors, like Monolog.

→ Download my free PHP Handbook!

THE VALLEY OF CODE

THE WEB DEVELOPER's MANUAL

You might be interested in those things I do:

  • Learn to code in THE VALLEY OF CODE, your your web development manual
  • Find a ton of Web Development projects to learn modern tech stacks in practice in THE VALLEY OF CODE PRO
  • I wrote 16 books for beginner software developers, DOWNLOAD THEM NOW
  • Every year I organize a hands-on cohort course coding BOOTCAMP to teach you how to build a complex, modern Web Application in practice (next edition February-March-April-May 2024)
  • Learn how to start a solopreneur business on the Internet with SOLO LAB (next edition in 2024)
  • Find me on X

Related posts that talk about php: