Skip to main content
The 2024 Developer Survey results are live! See the results
added 2662 characters in body
Source Link
mario
  • 145.1k
  • 20
  • 240
  • 293

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.

  1. 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 literal 1).

  1. 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.

  2. 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.

  3. 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.

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

The token T_LNUMBER refers to a "long" / number.

  1. 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
    

*

* Quite often [comes up](https://stackoverflow.com/questions/9571559/syntax-error-unexpected-t-lnumber-expecting-t-variable-or) for using `preg_replace`-placeholders `"$1"` in PHP context:

        #                         ↓            ⇓  ↓
        preg_replace("/#(\w+)/e",  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 literal 1).

  1. 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.

  2. 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.

  3. 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.

Active reading.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132

##Unexpected T_LNUMBER

INIn PHP, and most other programming languages, variables cannot start with a number. The first character must be alphabetic or an underscore.

$1   // badBad
$_1  // goodGood

##Unexpected T_LNUMBER

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

##Unexpected T_LNUMBER

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
Source Link
John Conde
  • 218.9k
  • 99
  • 461
  • 501

##Unexpected T_LNUMBER

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
Post Made Community Wiki by John Conde