Syntax errors

If a PHP program isn’t syntactically correct, interpreter will show a relevant message and a note with a file and line where an error might have occured. Syntax errors occur when the code has grammar mistakes. Grammar rules are important in human languages but the lack of it won’t affect your ability to read or understand any text. But in programming everything should be strict. Even a tiny violation and your program will not run. A wrong parenthesis order, a ; that you forgot to add — these are just some examples of such violations.

Here is an example of code with a syntax error:

<?php

print_r('Hodor')

If we run this code we will see the following message: $ PHP Parse error: syntax error, unexpected end of file in /private/var/tmp/index.php on line 4. In PHP such errors are called “Parse error”. As we can see it includes a file path and a line number.

On one hand, parse errors are the simplest because they are all about code grammar rules and aren’t related to any code logic. They can be easily fixed, you just need to find them.

On the other hand, interpreter will not always tell you the correct position of an error. Sometimes you need to add a forgotten bracket to another place as opposed to what the error message says.

instructions

Put on the screen What Is Dead May Never Die.


definitions
  • Syntax error — violation of grammar rules in a programming language

  • Parse error — type of errors in PHP, which occur when there are syntax errors in the code


Exercise available only for signed users.

Please sign in with your GitHub account, this is necessary to track the progress of the lessons. If you do not have an account yet, now is the time to create an account on Github.