Missing semicolon
It most commonly indicates a missing semicolon in the previous line. Variable assignments following a statement are a good indicator where to look:
⇓ func1() $var = 1 + 2; # parse error in line +2
String concatenation
A frequent mishap are string concatenations with forgotten
.
operator:⇓ print "Here comes the value: " $value;
Btw, you should prefer string interpolation (basic variables in double quotes) whenever that helps readability. Which avoids these syntax issues.
String interpolation is a scripting language core feature. No shame in utilizing it. Ignore any micro-optimization advise about variable
.
concatenation being faster. It's not.Missing expression operators
Of course the same issue can arise in other expressions, for instance arithmetic operations:
⇓ print 4 + 7 $var;
PHP can't guess here if the variable should have been added, subtracted or compared etc.
Lists
Same for syntax lists, like in array populations, where the parser also indicates an expected comma
,
for example:⇓ $var = array("1" => $val, $val2, $val3 $val4);
Or functions parameter lists:
⇓ function myfunc($param1, $param2 $param3, $param4)
Equivalently do you see this with
list
orglobal
statements, or when lacking a;
semicolon in afor
loop.Class declarations
This parser error also occurs in class declarations. You can only assign static constants, not expressions. Thus the parser complains about variables as assigned data:
class xyz { ⇓ var $value = $_GET["input"];
Unmatched
}
closing curly braces can in particular lead here. If a method is terminated too early (use proper indentation!), then a stray variable is commonly misplaced into the class declaration body.Variables after identifiers
You can also never have a variable follow an identifier directly:
⇓ $this->myFunc$VAR();
Btw, this is a common example where the intention was to use variable variables perhaps. In this case a variable property lookup with
$this->{"myFunc$VAR"}();
for example.Take in mind that using variable variables should be the exception. Newcomers often try to use them too casually, even when arrays would be simpler and more appropriate.
Missing parentheses after language constructs
Hasty typing may lead to forgotten opening or closing parenthesis for
if
andfor
andforeach
statements:⇓ foreach $array as $key) {
Solution: add the missing opening
(
between statement and variable.⇓ if ($var = pdo_query($sql) { $result = …
The curly
{
brace does not open the code block, without closing theif
expression with the)
closing parenthesis first.Else does not expect conditions
⇓ else ($var >= 0)
Solution: Remove the conditions from
else
or useelseif
.Need brackets for closure
⇓ function() usesuse $var {}
Solution: Add brackets around
$var
.Invisible whitespace
As mentioned in the reference answer on "Invisible stray Unicode" (such as a non-breaking space), you might also see this error for unsuspecting code like:
<?php ⇐ $var = new PDO(...);
It's rather prevalent in the start of files and for copy-and-pasted code. Check with a hexeditor, if your code does not visually appear to contain a syntax issue.
Peter Mortensen
- 31.4k
- 22
- 109
- 132
Add #9 "Need brackets for closure" & add "elseif" alternative for #8 "Else does not expect conditions"
Chris Happy
- 7.2k
- 2
- 26
- 51