Why variable can not start with a Number?

試著忘記壹切 提交于 2019-11-29 11:15:41

This is how PHP was designed. You can't start a variable name with a number.

What you can do is use underscore:

$_6 = $_REQUEST['6'];

EDIT: since variables start with $ in PHP, is would be possible to have variables starting with numbers, but that would be a bit confusing to most people since there is no other language that allows variables starting with numbers (or at least I don't know any).

But let's imagine variables starting with numbers are allowed.

Can you imagine a coworker saying to you: 23 equals 74? That is a bit confusing. Hearing n23 equals 74 makes more sense. You know n23 is a variable without having to explicitly say that.

Yes you can declare variables in PHP that start with a number:

${'1'} = 'hello';

This declares the variable with the variable name "1" in PHP.

To access it, you need to use the brace syntax again:

echo ${'1'};

This is necessary because in PHP code verbatim you can not write:

echo $1;

That would give a syntax error.


Also can I create and use fields in Mysql with the same name?

Yes you can, it works similarly as in PHP. In Mysql as well you need to quote the number-only identifier for the column, the quoting differs:

mysql> create table test (`0` INT);
mysql> describe test;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| 0     | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+

Here the backticks are used (default Mysql configuration). Both the PHP manual and the Mysql manual explain the details of this, also there are many related questions to quoting identifiers both for PHP and Mysql on the Stackoverflow website.

It does not make sense. But you can still use the dynamic names of variables.

<?php

$name = 1;

$ $name = 'name';
// $ 1 = 'name'; // the same, but syntax not allowed

var_dump($ $name); // value of $1
var_dump(${$name});
var_dump($$name);
Sankalp Mishra

Well think about this:

$2d = 42;
$a = 2d;

What is a? 2.0? or 42?

Hint, if you don't get it, d after a number means the number before it is a double literal

There being difficulty in unambiguosly determining whether a numeric character in the compilation unit represented a literal or an identifier.

Languages where space is insignificant (like ALGOL and the original FORTRAN if I remember correctly) could not accept numbers to begin identifiers for that reason.

This goes way back - before special notations to denote storage or numeric base.

For a more detailed discussion visit Why can't variable names start with numbers?

You cannot define a variable starts with numerics in PHP. Take a look at the below link. It has the basics of defining variables.

PHP Variables - Basics

This is some kind of a legacy, starting with earlier-developed languages like C, Fortran etc. Also some shell script languages have special means for identifiers that start with a digit, namely input parameters. PHP standards simply decided to comply with this general rule.

As the php documentation says, you can't :

Basics

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

Concerning MySQL, the documentation says :

Identifiers may begin with a digit but unless quoted may not consist solely of digits.

You can't name a variable starting with a number (or just a number for that matter). What you can do is use another character to start (for instance, and underscore, or a 3 lettercombo saying what type of variable it is:

$_1 = $_REQUEST['1'];

or

$str1 = $_REQUEST['1'];

What you can also do is use an array:

$retrievedValuesFromDB = array();
$retrievedValuesFromDB['1'] = $_REQUEST['1'];

This way you can loop through both at the same time:

$i=1;
while ($i<mysql_num_rows())
{
    $retrievedValuesFromDB[$i] = $_REQUEST[$i];
    $i++;
}

To augment @hakre's answer you could also lose the quotes for brevity

${1}

but alas, you can not combine that with a string value. I did some experiments and apparently PHP expects either a string or a number value. And you can do anything within the curly brackets that eventually returns a number or a string value or is implicitly cast to either.

<?php
function eight (){
   return 8;
}
${7+'fr'} = ${7*'fr'+1} = ${'hi'.((string)8).'hi'} = ${7.8910} = ${eight()} = ${(eight()==true)+4} = 'hi';

var_dump(get_defined_vars());

Outputs

array(8) { ["_GET"]=> array(0) { } 
["_POST"]=> array(0) { } 
["_COOKIE"]=> array(0) { } 
["_FILES"]=> array(0) { } 
["7.891"]=> string(2) "hi" 
["7"]=> string(2) "hi" 
["1"]=> string(2) "hi" 
["hi8hi"]=> string(2) "hi"
["8"]=> string(2) "hi" 
["5"]=> string(2) "hi" } 

Addtionally I found something interesting:

<?php
${false} = 'hi';
${true} = 'bye';

var_dump(get_defined_vars());

Outputs

array(6) { ["_GET"]=> array(0) { } 
["_POST"]=> array(0) { } 
["_COOKIE"]=> array(0) { }
[""]=> string(2) "hi" 
["1"]=> string(3) "bye" } 

Which means false evaluates to an empty string and not 0 and true to 1.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!