Errors: read, reproduce, fix
An error message is the shortest explanation a PHP program ever gives. This lesson makes one visible, sends it to a log, catches it with try and reads a real case from the error collection.
Show errors locally
Enable error display only on a local machine. Set display_errors to 1 and error_reporting to E_ALL.
You will see warnings and notices right in the output.
<?php ini_set("display_errors", "1"); error_reporting(E_ALL); echo "Errors are visible", PHP_EOL;
php display.php
Errors are visible
The two calls change nothing about this clean program, and that is the point: the next mistake it meets will print its warning among these lines instead of disappearing.
Log instead of display
Disable display on production and write to a log instead. Set display_errors to 0 and log_errors to 1.
Inspect the log file after each request to find hidden issues.
<?php ini_set("display_errors", "0"); ini_set("log_errors", "1"); error_log("Something went wrong"); echo "request done", PHP_EOL;
php log.php
Something went wrong
request done
The logged line leaves through the error channel, which the terminal shows before the normal output, and the page itself only prints the last line. On a web server that channel is the log file.
try and catch
Wrap risky calls in a try block. List each failure type in its own catch block.
The program continues after catch instead of stopping.
<?php try { throw new RuntimeException("Demo failure"); } catch (RuntimeException $e) { echo $e->getMessage(), PHP_EOL; } echo "after catch", PHP_EOL;
php try.php
Demo failure
after catch
The throw leaves the try block at once, the catch of the matching class takes over, and the line after it still runs, which an uncaught exception would not allow.
Real cases from this site
Real projects fail on bad JSON, missing files and wrong types.
Study the collection
PHP errors: case studies
for step by step fixes. Reproduce each case locally before changing code.
<?php try { $result = json_decode("{broken}", true, 512, JSON_THROW_ON_ERROR); } catch (JsonException $e) { echo $e->getMessage(), PHP_EOL; } echo "after catch", PHP_EOL;
php json.php
Syntax error
after catch
The flag JSON_THROW_ON_ERROR turns the quiet null of the JSON lesson into an exception, so the same broken text now names its problem and lets the program decide what to do next.
Exercises
- Add a second ini_set that sets display_startup_errors to 1 and run display.php. What changes in the output?
- Print an undefined variable in display.php after the settings. Which warning appears, and in which line?
- Throw an InvalidArgumentException in try.php and see which catch takes it. Add a second catch for it and run again.
- Remove the JSON_THROW_ON_ERROR flag from json.php and run it. What does $result hold now, and does the catch run?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where should
display_errorsbe 1?Answer
On a local machine only
A displayed error can show paths and values to a visitor, so production logs it instead.
-
What does
log_errors = 1do?Answer
It sends the errors to the error log instead of the output
The error goes to the log channel, which is a file on a web server and the terminal in a command line run.
-
What happens to a program after a
catchblock finishes?Answer
It continues with the line after the try and catch
A caught exception is handled, so the normal flow goes on after the whole construct.
-
What does
JSON_THROW_ON_ERRORchange aboutjson_decode()?Answer
It throws a JsonException instead of returning null
The flag replaces the quiet null with an exception that a catch block can read.
-
What does this program print?
<?php try { throw new RuntimeException("Demo failure"); } catch (RuntimeException $e) { echo $e->getMessage(); }Answer
Demo failure
The catch takes the exception and getMessage returns the text it was thrown with.
-
What does this program print?
<?php try { throw new RuntimeException("caught"); } catch (RuntimeException $e) { echo $e->getMessage(); } echo "continued";Answer
caughtcontinued
The message prints without a line break and the next echo follows it, so the two words run together.
-
What does this program print?
<?php try { throw new InvalidArgumentException("bad"); } catch (InvalidArgumentException $e) { echo "invalid"; } catch (RuntimeException $e) { echo "runtime"; }Answer
invalid
The first catch whose class matches the exception takes it, and InvalidArgumentException is listed first.
-
What does this program print?
<?php try { $result = json_decode("{broken}", true, 512, JSON_THROW_ON_ERROR); } catch (JsonException $e) { echo $e->getMessage(); }Answer
Syntax error
The flag throws a JsonException whose message is Syntax error, and the catch prints it.
-
Which function writes a line to the error log?
Answer
error_log()error_log sends its message to the log channel; echo and print_r write to the output.
-
Why is a fatal error different from a warning?
Answer
A fatal error stops the script, a warning lets it continue
The levels differ in what they do to the run: a warning reports a problem and the program goes on, a fatal error ends it.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev