Pretty universally have you missed a semicolon in the previous line if the parser complains about if or foreach, for, while, list, global, return, do, print, echo etc. being misplaced:
⇓
$x = myfunc()
if (true) {
Solution: look into the previous line; add semicolon.Class declarations
Another location where this occurs is in class declarations. In the class section you can only list property variable initializations and function/method sections. No code may reside there.class xyz {
if (true) {}
foreach ($var) {}
Commonly happens if one of the contained methods got } closed too early, due to wrongly nested code blocks. (Solution: use code indentation and an IDE to check!)Language statements in expression context
Many language constructs can only be used as statements. They are not to be placed inside other expressions: ⇓
$var = array(1, 2, foreach($else as $_), 5, 6);
PHP doesn't have list comprehensions, nor $_, and foreach wouldn't be used either.Likewise can't you use an if in strings, math expressions or elsewhere:
⇓
print "Oh, " . if (true) { "you!" } . " won't work";
// Use a ternary statement here instead, when versed enough.
Same applies to for, while, global, echo and a lesser extend list. ⇓
echo 123, echo 567, "huh?";
Whereas print() is a language construct that could be used in expression context.
No comments:
Post a Comment