Unexpected T_CONSTANT_ENCAPSED_STRING, Unexpected T_ENCAPSED_AND_WHITESPACE
The unwieldy namesT_CONSTANT_ENCAPSED_STRING
and T_ENCAPSED_AND_WHITESPACE
refer to quoted "string"
literals.They're used in different contexts, but the syntax issue are quite similar. T_ENCAPSED… warnings occur in double quoted string context, while T_CONSTANT… strings are often astray in plain PHP expressions or statements.
Incorrect variable interpolation
And it comes up most frequently for incorrect PHP variable interpolation:
Quoting arrays keys is a must in PHP context. But in double quoted strings (or HEREDOCs) this is a mistake. The parser complains about the contained single quoted⇓ ⇓ echo "Here comes a $wrong['array'] access";
'string'
, because it usually expects a literal identifier / key there.
More precisely it's valid to use PHP2-style simple syntax within double quotes for array references:
Nested arrays or deeper object references however require the complex curly string expression syntax:echo "This is only $valid[here] ...";
If unsure, this is commonly safer to use. It's often even considered more readable. And better IDEs actually use distinct syntax colorization for that.echo "Use {$array['as_usual']} with curly syntax.";
Missing concatenation
If a string follows an expression, but lacks a concatenation or other operator, then you'll see PHP complain about the string literal:
While it's obvious to you and me, PHP just can't guess that the string was meant to be appended there.⇓ print "Hello " . WORLD " !";
Confusing string quote enclosures
The same syntax error occurs when confounding string delimiters. A string started by a single'
or double"
quote also ends with the same.
That example started with double quotes. But double quotes were also destined for the HTML attributes. The intended concatenation operator within however became interpreted as part of a second string in single quotes.⇓ print "<a href="' . $link . '">click here</a>"; ⌞⎽⎽⎽⎽⎽⎽⎽⎽⌟⌞⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⌟⌞⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⌟
Tip: Set your editor/IDE to use slightly distinct colorization for single and double quoted strings. (It also helps with application logic to prefer e.g. double quoted strings for textual output, and single quoted strings only for constant-like values.)
This is a good example where you shouldn't break out of double quotes in the first place. Instead just use proper\"
escapes for the HTML attributes´ quotes:
While this can also lead to syntax confusion, all better IDEs/editors again help by colorizing the escaped quotes differently.print "<a href=\"{$link}\">click here</a>";
Missing opening quote
Equivalently are forgotten opening"
/'
quotes a recipe for parser errors:
Here the⇓ make_url(login', 'open');
', '
would become a string literal after a bareword, when obviouslylogin
was meant to be a string parameter.Array lists
If you miss a,
comma in an array creation block, the parser will see two consecutive strings:
Note that the last line may always contain an extra comma, but overlooking one in between is unforgivable. Which is hard to discover without syntax highlighting.array( ⇓ "key" => "value" "next" => "....", );
Function parameter lists
Same thing for function calls:
⇓ myfunc(123, "text", "and" "more")
Runaway strings
A common variation are quite simply forgotten string terminators:
Here PHP complains about two string literals directly following each other. But the real cause is the unclosed previous string of course.⇓ mysql_evil("SELECT * FROM stuffs); print "'ok'"; ⇑