Fifteen easy solutions to identify, handle and parse PHP Syntax Errors efficiently

One of the annoying problem most web developers encounter is PHP Syntax Errors.

What are PHP Syntax Errors?

PHP syntax errors are the errors that is caused due to implementation of wrong syntax in the PHP Script. PHP is a server side scripting language. Therefore, we have to careful while using this scripting language in our php script. Most common reasons behind syntax errors are a statement is not ending with semi colon, one of the parenthesis is missed, or variable declaration sign is missing etc.

Precautions to avoid PHP Syntax Errors:

PHP Syntax Error Handling:

Before handling the syntax error, it is good practice to back up the php script for later reference.

Solution 01: The best part about PHP Syntax errors, tells us about the error present in the PHP Script.

Example:

Parse error: syntax error, unexpected T_STRING, expecting ‘;’ in excess_employee_info.php on line 3255

The above syntax error tells us about the type of error, a possible solution, the file name and code line which contains the error.

In the above example, the type of error is syntax error, which is unexpected T_STRING. The solution could be to add semi colon, perhaps semi colon is missing. It tells the php file name, excess_employee_info.php. And the code line which contains the error is 3255.

(Note: Not neccessarily, missing semicolon could have caused the syntax error and adding semicolon would solve the issue. But you have to analyse the code line along with previous codelines and after code lines, then only you would able to identify the root reason behind the error.)

Solution 02: If code blocks are not closed properly, then also syntax errors rise. Triple check whether the code blocks are properly closed.

Solution 03: IDE Softwares or advanced CPANEL php editing page, colorises variables, code blocks, functions, comments in certain way so that while reading the script we could easily identify a variable, code block, comments. If something is not right in the php script, it would be easily noticeable.

Solution 04: Check whether operators are not used in improper way.

Solution 05: Two strings or identifiers directly following each other are incorrect in most cases. So look if it is present in the php script.

Solution 06: If for any reason, we are using long line of string then it would be wise to break it down into multiple lines. We can use concatenation operator to combine multiple line of string.

Solution 07: Check whether you have missed comment declaration. It would also rise the syntax error in PHP Script.

//lskdfjlskdjf lksjdf lksjdf ->Single line comment

/*

sldkjfsldfj

sldkfjslkdf

lskdjfslkdf

*/ -> Mutliline comments

Solution 08: If else condition code block is large and complex, then break it into multiple if else condition. Nested if else condition blocks would be beneficial. Easy to track down the error if any rises.

Solution 09: If we unable to locate the root reason behind the syntax error, then we can comment out the suspicious codes which might be causing the problem. This way also we can track down the root reason. But commenting out certain codes can also rise new errors.

Solution 10: If still unable to identify the reason behind the error, then temporarily remove functions which you doubt might be causing the error.

Solution 11: Most new web developers who are migrated from other programming language, might find difficult with if(condition):, if(condition): else:, while(condition): So avoid such usage of if condition or while condition. Rather use if(condition){}, while(condition){}

Solution 12: Another possible reason that might be causing the syntax error is mismatched string quotes.

Example:

echo(“hello world’);

The above code would rise an error

echo “<h2 style=”color:red;”>Hello world</h2>”;

The above code would raise syntax error. The right syntax would be:

echo ‘<h2 style=”color:red;”>Hello world</h2>’;

(In the first syntax, php parser confuses with double quotes)

Solution 13: Forgot to use concatenation operator could also raise error.

Example:

echo “The salary of the employee: “$sal;

The above code would raise error because it is missing concatenation operator.

The right syntax would

echo “The salary of the employee: “.$sal; //note the concatenation operator in between the string and value.

Solution 14: Atlast, if nothing helps, then double check, are you verifying the required php script. Sometimes unknowingly, we might be digging into other php script instead of the required one.

Solution 15: Avoid using of PHP Reserved keywords as function name, class name or constant name.

If every attempt fails, then no need to worry, Guru Google is there. Just look into it with the raised error message and plenty of solution would rain on the screen.