We'll collect matching parenthesis and curly brace related errors here. While the causes are diffuse, there's also some overlap.
unexpected '(
' (parenthesis)
Opening parens typically follow language constructs like if
/foreach
/for
/array
/list
or start an arithmetic subexpression. They're syntactically incorrect after "strings"
, a previous ()
, a lone $
, and in some typical declaration contexts.
Function declaration parameters
A unlikely occurence for this error is trying to use expressions as default function parameters, which is not supported:
function header_fallback($value, $expires = time() + 90000) {
Parameters in a function declaration can only be literal values. Unlike for function invocations, where you can freely use whatever(1+something()*2)
etc.
Class property defaults
Same thing for class member declarations, where only literal/constant values are allowed, not expressions:
class xyz { ⇓
var $default = get_config("xyz_default");
Put such things in the constructor.
See also Why don't PHP attributes allow functions?
Javascript syntax in PHP
Utilizing Javascript or jQuery syntax won't work in PHP for obvious reasons:
<?php ⇓
print $(document).text();
isset(()), empty, key, next, current
Both isset()
and empty()
are language built-ins, not functions. They need to access a variable directly. If you inadvertently add a pair of parentheses too much, then you'd create an expression however:
⇓
if (isset(($_GET["id"]))) {
Same applies to any language construct that requires implicit variable name access. User-level functions that require a variable reference -but get an expression result passed- lead to runtime errors instead. But these built-ins are part of the language grammer, therefore allow no ornamentation.
unexpected ')
'
Absent function parameter
You cannot have stray commas last in a function call. PHP expects a value there and thusly complains about an early closing )
paren.
⇓
callfunc(1, 2, );
A trailing comma is only allowed in array()
or list()
constructs.
Unfinished expressions
If you forget something in an arithmetic expression, then the parser gives up. But neither could you possibly answer:
⇓
$var = 2 * (1 + );
And if you forgot the closing )
even, then you'd get a complaint about the unexpected semicolon instead.
Foreach as constant
For forgotten variable $
prefixes in control statements you will see:
⇓
foreach ($array as wrong) {
PHP here often tells you it expected a ::
instead, because a class::$variable would have been the only alternative to a local $variable right in the foreach declaration.
unexpected '{
' (curly braces)
{
and }
enclose code blocks, and syntax errors usually pertain to that. Curly braces are also used in variable variables and complex variable expressions (double quoted string context).
Unmatched subexpressions in an if
Most commonly unbalanced (
and )
are the cause if the parser complains about the opening curly {
for code blocks appearing too early. A simple example:
⇓
if (($x == $y) && (2 == true) {
Count your parens or use an IDE which does that for you. Also don't write code without readability-aiding whitespace.
{ and } in expression context
You can't use curly braces in expressions. If you confuse parentheses and curlys, it won't comply to the language grammer:
⇓
$var = 5 * {7 + $x};
There are a few exceptions for identifier construction, such as local scope variable ${references}
or variable variables.
unexpected '}
'
Closing a code block can pretty much only be too early, if you have an unclosed expression or the last statement in a function/code block without trailing ;
semicolon:
function whatever() {
doStuff()
} ⇧
Otherwise the parser can't tell if you perhaps still wanted to add + 25;
to the function result or something else.
invalid code block nesting
Quite as often do you see this parser error when a code block was }
closed too early, or you forgot an opening {
even:
function doStuff() {
if (true) ⇦
print "yes";
}
} ⇧
In above snippet the if
didn't have an opening {
curly brace. Thus the closing }
one below it was redundant. And therefore then the closing }
intended for the function was not associatable to a previous opening {
.
Such errors are even harder to find without proper code indentation. But IDEs help with it.
unexpected '{', expecting '('
Language constructs which require a condition/declaration header and a code block will trigger this error. For example misdeclared functions without parameter list are not permitted:
⇓
function whatever {
}
And you can't likewise have an if
without condition:
⇓
if {
}
Same thing for the usual suspects, for
/foreach
and while
/do
etc.