Sometimes we see the ‘Parse Error syntax error unexpected … ‘ when we edit WordPress files or we install an new theme or plugin. This error can be frustrating and take your website down. So let check the main reasons why this happen.
- Parse error: syntax error, unexpected T_STRING
- Parse error: syntax error, unexpected T_VARIABLE
- Parse error: syntax error, unexpected T_IF …
All these messages, in fact, are different examples of the same error: the PHP compiler warns us of having found a type of construct that was not expected or trusted. These errors are almost always the result of a typing error. For example:
if $ xyz == 1) echo 'one';
In the example above we forgot to open the round bracket after the if. In this case we will get unexpected T_VARIABLE because the interpreter found himself facing a variable – $ xyz – that he didn't expect.
Any error message that includes a word that begins with T_ refers to a token, that is, an internal representation of a language construct. For example, T_VARIABLE is the token used by PHP to indicate a variable. Similarly, T_STRING is the token that represents a string, and so on. The token list is available on this page of the official PHP documentation.
In general, these can be some of the most difficult errors to trace because often the line number indicated in the error could be very far from the line where the actual error was made. It is therefore a good idea to regularly run tests to run the script during the programming phase in order to find the error on a limited portion of the code, otherwise the risk is to have to look for a needle in a haystack.
Parse error: syntax error, unexpected $ end
This error is related to the errors mentioned above, but it is worth treating it separately. This error message informs us that the PHP interpreter arrived at the end of the file but was still expecting something (that was not there). Very often this error is caused by the lack of a “}” in the code. Unfortunately you will have to scan the entire file to find out where the mistake was made …
This error can also be generated by bad syntax related to the heredoc construct. The heredoc syntax requires that the final delimiter must be the first element of the line, without any other character, including spaces, that precedes it. Often, due to typos or editors attempting to do automatic code formatting, this rule is not respected and this produces this error.
Parse error: syntax error, unexpected ‘=', expecting ‘;'
This error is also part of the same family of errors seen above. In this circumstance the PHP interpreter is very precise in informing us of what we were expecting (and did not find) for example an equal for assigning a value to a variable or a semicolon as a terminator of an intrusion .
Debugging this type of error is a bit simpler than the previous one because PHP usually indicates a line of code rather close to the exact point where the error was made.
Normally to find the issue you have to find the exact line where the problem is through the error you see on the front page:
As you see in the error message above you have to edit the file single.php on the line 43 you can find one of the issue i had described above. If you website is published online you have to access the ftp service to edit the file.
Ludjon, who co-founded Codeless, possesses a deep passion for technology and the web. With over a decade of experience in constructing websites and developing widely-used WordPress themes, Ludjon has established himself as an accomplished expert in the field.
I just want to say I am newbie to blogging and site-building and seriously loved you’re web site. Most likely I’m going to bookmark your blog. You amazingly come with fabulous articles and reviews. Thanks a lot for sharing your web site.