Misquoted strings
This syntax error is most common for misquoted string values however. Any unescaped and stray
"
or'
quote will form an invalid expression:⇓ ⇓ echo "<a href="http://example.com">click here</a>";
Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping
\"
double quotes, or\'
single quotes - depending on which was used as string enclosure.- For convenience you should prefer outer single quotes when outputting plain HTML with double quotes within.
- Use double quoted strings if you want to interpolate variables, but then watch out for escaping literal
"
double quotes. - For lengthier output, prefer multiple
echo
/print
lines instead of escaping in and out. Better yet consider a HEREDOC section.
Another
Another example is using PHP entry inside HTML code generated with PHP:$text = '<div>some text with <?php echo 'some php entry' ?></div>'
This happens if
$text
is large with many lines and developer does not see the whole PHP variable value and focus on the piece of code forgetting about its source. Example is hereSee also What is the difference between single-quoted and double-quoted strings in PHP?.
Unclosed strings
If you miss a closing
"
then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:⇓ echo "Some text", $a_variable, "and some runaway string ; success("finished"); ⇯
It's not just literal
T_STRING
s which the parser may protest then. Another frequent variation is anUnexpected '>'
for unquoted literal HTML.Non-programming string quotes
If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:
$text = ’Something something..’ + ”these ain't quotes”;
Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example
”these
is interpreted as a constant identifier. But any following text literal is then seen as a bareword/T_STRING by the parser.The missing semicolon; again
If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:
⇓ func1() function2();
PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one
||
or the other.Short open tags and
<?xml
headers in PHP scriptsThis is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:
⇓ <?xml version="1.0"?>
PHP will see the
<?
and reclaim it for itself. It won't understand what the strayxml
was meant for. It'll get interpreted as constant. But theversion
will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.Invisible Unicode characters
A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:
<?php print 123;
You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.
You can narrow it down with with adding redundant
;
statement separators at each line start:<?php ;print 123;
The extra
;
semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.The `$` sign missing in front of variable names
Variables in PHP are represented by a dollar sign followed by the name of the variable.
The dollar sign (
$
) is a sigil that marks the identifier as a name of a variable. Without this sigil, the identifier could be a language keyword or a constant.This is a common error when the PHP code was "translated" from code written in another language (C, Java, JavaScript, etc.). In such cases, a declaration of the variable type (when the original code was written in a language that uses typed variables) could also sneak out and produce this error.
Escaped Quotation marks
If you use
\
in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.Example:
echo 'Jim said \'Hello\'';
will printJim said 'hello'
If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.
Very common error when specifiying paths in Windows:
"C:\xampp\htdocs\"
is wrong. You need"C:\\xampp\\htdocs\\"
.Typed properties
You need PHP ≥7.4 to use property typing such as:
public stdClass $obj;
You can narrow it down with with adding redundant ;
statement separators at each line start:
<?php
;print 123;
The extra ;
semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.
The `$` sign missing in front of variable names
Variables in PHP are represented by a dollar sign followed by the name of the variable.
The dollar sign ($
) is a sigil that marks the identifier as a name of a variable. Without this sigil, the identifier could be a language keyword or a constant.
This is a common error when the PHP code was "translated" from code written in another language (C, Java, JavaScript, etc.). In such cases, a declaration of the variable type (when the original code was written in a language that uses typed variables) could also sneak out and produce this error.
Escaped Quotation marks
If you use \
in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.
Example: echo 'Jim said \'Hello\'';
will print Jim said 'hello'
If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.
Very common error when specifiying paths in Windows: "C:\xampp\htdocs\"
is wrong. You need "C:\\xampp\\htdocs\\"
.