Function declaration parameters
A rarer occurrence for this error is trying to use expressions as default function parameters. This is not supported, even in PHP7:
function header_fallback($value, $expires = time() + 90000) {
Parameters in a function declaration can only be literal values or constant expressions. 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?
Again note that PHP 7 only allows
var $xy = 1 + 2 +3;
constant expressions there.JavaScript syntax in PHP
Using JavaScript or jQuery syntax won't work in PHP for obvious reasons:
<?php ⇓ print $(document).text();
When this happens, it usually indicates an unterminated preceding string; and literal
<script>
sections leaking into PHP code context.isset(()), empty, key, next, current
Both
isset()
andempty()
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"]))) {
The same applies to any language construct that requires implicit variable name access. These built-ins are part of the language grammar, therefore don't permit decorative extra parentheses.
User-level functions that require a variable reference -but get an expression result passed- lead to runtime errors instead.
Curly braces {
and }
enclose code blocks. And syntax errors about them usually indicate some incorrecincorrect nesting.
Unmatched subexpressions in an
if
Most commonly unbalanced
(
and)
are the cause if the parser complains about the opening curly{
appearing too early. A simple example:⇓ if (($x == $y) && (2 == true) {
Count your parensparentheses or use an IDE which helps with that. Also don't write code without any spaces. Readability counts.
{ 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 grammergrammar:
⇓ $var = 5 * {7 + $x};
There are a few exceptions for identifier construction, such as local scope variable
${references}
.Variable variables or curly var expressions
This is pretty rare. But you might also get
{
and}
parser complaints for complex variable expressions:⇓ print "Hello {$world[2{]} !";
Though there's a higher likelihood for an unexpected
}
in such contexts.
Last statement in a code block
It can happen for any unterminated expression.
And if the last line in a function/code block lacks a trailing
;
semicolon:function whatever() { doStuff() } ⇧
Here the parser can't tell if you perhaps still wanted to add
+ 25;
to the function result or something else.Invalid block nesting / Forgotten
{
You'll sometimes 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 became redundant. And therefore the next closing}
, which was intended for the function, was not associatableassociable to the original opening{
curly brace.Such errors are even harder to find without proper code indentation. Use an IDE and bracket matching.