Errors in a program
In the previous lesson, PHP refused to run a file because of a missing comma. Back to the cook with the recipe card: the work can go wrong for three different reasons.
- The page is torn, and the recipe cannot be read. The cook does not start cooking at all.
- The cook reaches the step "add saffron", and there is no saffron in the kitchen. The steps before it are done, and the work stops there.
- A step says "2 spoons of salt or so". The cook adds 2 spoons, leaves a note in the margin and keeps cooking.
PHP has errors of all three kinds, and the first word of its message names the kind. Let's go through them one by one.
Three kinds of errors
Parse error
Before the run, PHP reads the whole file and checks that the text follows the rules of the language. If a rule is broken, that is a parse error, and not a single line runs, not even the first:
<?php
echo "One\n";
echo "Two\n";
echo "Three\n" // ✗Parse error: syntax error, unexpected end of file, expecting "," or ";"The error sits on the last line, yet not even One is printed. PHP found it while parsing, before the run. The words unexpected end of file mean that PHP expected a comma or a semicolon, and the file ended instead.
Fatal error
A fatal error happens while the program runs: PHP reaches a line and cannot run it. The lines above it have already run, and the lines below it will not:
<?php
echo "Hi\n"; // Hi
echo Hello, "\n"; // ✗
echo "Bye\n";Fatal error: Uncaught Error: Undefined constant "Hello"The word Hello has no quotes. Where echo expects a value, PHP reads a word without quotes as the name of a constant, a value the program has to declare beforehand. Such a line breaks no rule of the language, so parsing passes, and the program prints Hi.
On the line with Hello, PHP looks for that constant, does not find it and stops the program. The words Undefined constant mean "unknown constant", and Bye never gets printed.
Here the cook picture stops working. A real cook would find a substitute for the saffron or skip the step, while on such a line PHP looks for no substitute and stops.
Warning
A warning is a message that something is wrong in a line, yet PHP managed to run it. After a warning, the program goes on:
<?php
echo "One\n"; // One
echo "5 apples" + 1, "\n"; // ✗
echo "Three\n"; // ThreeWarning: A non-numeric value encounteredLet's see what happened:
- The first line prints
One. - In the second line,
+adds a number to text. PHP takes the5from the start of the text"5 apples"and prints6, but first it prints a warning: "a non-numeric value encountered". - The third line runs as usual and prints
Three.
PHP prints a warning right into the output, between the lines of the program. The result 6 may not be what you expected, so warnings are worth reading and fixing.
Telling them apart by the output
Interviewers often ask how a warning differs from a fatal error. You can tell the kind of error by the message and by what the program managed to print:
Parse errorand empty output: a parse error, the file never ran;Fatal error, the lines above printed and the ones below did not: a fatal error, the program stopped at that line;Warningand every line printed: a warning, the program reported a problem and ran to the end.
How to read the message
PHP's message is made of parts:
- At the start, the kind of error:
Parse error,Fatal errororWarning. - After the colon, the description: what exactly is wrong.
- At the end of the first line, the path to the file and the line number:
in /home/anna/hello.php on line 4, or, in a fatal error, the shorterin /home/anna/hello.php:4. - A fatal error is followed by more lines,
Stack trace:andthrown in. In your first programs you can skip them.
The words Uncaught Error in a fatal error mean "an error nobody caught": PHP reported an error, the program did not handle it and therefore stopped. In the course's lessons, only the first line of a message is kept, without the path and the line number, as in the examples above.
Common mistake
People look for the error on the line PHP names, but it sits above it:
<?php
echo "One\n";
echo "Two\n" // ✗
echo "Three\n";Parse error: syntax error, unexpected token "echo", expecting "," or ";"At the end of the full message, PHP writes on line 5, although nothing is wrong with line 5. The semicolon is missing at the end of line 4, but PHP noticed only when it met the next echo where it expected , or ;. If the line from the message looks right, check the end of the line above it.
Key points
- PHP finds a parse error before the run, so not a single line runs.
- A fatal error stops the program at its line: the lines above have run, and the ones below have not.
- After a
Warning, the program keeps running. - The first word of a message names the kind of error, and a missing
;is found at the end of the line above the one PHP names.