In PHP, and most other programming languages, variables cannot start withThe token T_LNUMBER
refers to a "long" / number. The first character must be alphabetic or an underscore.
Invalid variable names
In PHP, and most other programming languages, variables cannot start with a number. The first character must be alphabetic or an underscore.
$1 // Bad $_1 // Good
$1* Quite often [comes up](https://stackoverflow.com/questions/9571559/syntax-error-unexpected-t-lnumber-expecting-t-variable-or) Badfor using `preg_replace`-placeholders `"$1"` in PHP context:
$_1
# ↓ ⇓ ↓
preg_replace("/#(\w+)/e", Good strtopupper($1) )
Where the callback should have been quoted. (Now the `/e` regex flag has been deprecated. But it's sometimes still misused in `preg_replace_callback` functions.)
The same identifier constraint applies to object properties, btw.
↓ $json->0->value
While the tokenizer/parser does not allow a literal
$1
as variable name, one could use${1}
or${"1"}
. Which is a syntactic workaround for non-standard identifiers. (It's best to think of it as a local scope lookup. But generally: prefer plain arrays for such cases!)Amusingly, but very much not recommended, PHPs parser allows Unicode-identifiers; such that
$➊
would be valid. (Unlike a literal1
).
Stray array entry
An unexpected long can also occur for array declarations - when missing
,
commas:# ↓ ↓ $xy = array(1 2 3);
Or likewise function calls and declarations, and other constructs:
func(1, 2 3);
function xy($z 2);
for ($i=2 3<$z)
So usually there's one of
;
or,
missing for separating lists or expressions.Misquoted HTML
And again, misquoted strings are a frequent source of stray numbers:
# ↓ ↓ echo "<td colspan="3">something bad</td>";
Such cases should be treated more or less like Unexpected T_STRING errors.
Other identifiers
Neither functions, classes, nor namespaces can be named beginning with a number either:
↓ function 123shop() {
Pretty much the same as for variable names.