# How to deal with errors in PHP

> Learn how PHP handles warnings, notices, and errors, where to find them, and how to configure display_errors and the error log in your php.ini file.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2022-08-24 | Topics: [PHP](https://flaviocopes.com/tags/php/) | Canonical: https://flaviocopes.com/php-errors/

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
<?php
phpinfo();
?>
```

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

![phpinfo output showing PHP Version 8.1.0 and Loaded Configuration File path](https://flaviocopes.com/images/php-errors/Screen_Shot_2022-06-27_at_13.42.41.jpg)

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:

![MAMP file browser showing PHP 8.1.0 conf folder with php.ini file highlighted](https://flaviocopes.com/images/php-errors/Screen_Shot_2022-06-27_at_12.11.28.jpg)

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`:

![php.ini file open in editor showing display_errors setting with documentation](https://flaviocopes.com/images/php-errors/Screen_Shot_2022-06-27_at_12.13.16.jpg)

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:

![MAMP logs folder showing php_error.log file highlighted among other log files](https://flaviocopes.com/images/php-errors/Screen_Shot_2022-06-27_at_12.16.01.jpg)

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

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

![php.ini file showing error_log setting configured to MAMP logs directory path](https://flaviocopes.com/images/php-errors/Screen_Shot_2022-06-27_at_12.17.12.jpg)

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

![php_error.log file content showing multiple PHP Parse error messages with timestamps](https://flaviocopes.com/images/php-errors/Screen_Shot_2022-06-27_at_12.17.55.jpg)

You can add information to the error log by using the [`error_log()`](https://www.php.net/manual/en/function.error-log.php) function:

```php
error_log('test');
```

It’s common to use a logger service for errors, like [Monolog](https://github.com/Seldaek/monolog).
