Why can't a variable start with a number?

I work in PHP, I need to define variables in a sequence in order to save in Mysql. Both the field name and the variable name should be the same in my case.

Is it possible to declare a type variable

$1 OR $2 etc 

If not, why not, and if so, why?

I tried:

$id = 0;
$6  = $_REQUEST['6'];
$7  = $_REQUEST['7'];
$8  = $_REQUEST['8'];

$xary = array('6','7','8','9')

$oAppl->save_record("tablename", $id, "id");

which give me an error.

Can I also create and use fields in Mysql with the same name?

+5
source share
9 answers

This is how PHP was designed. You cannot run a variable name with a number.

What you can do is use underscore:

$_6 = $_REQUEST['6'];

EDIT: $ PHP, , , , , , , ( , ).

, .

, : 23 74?. . n23 74 . , n23 - .

+14

, PHP, :

${'1'} = 'hello';

"1" PHP.

, :

echo ${'1'};

, PHP- :

echo $1;

.


Mysql ?

, , PHP. Mysql , :

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

( Mysql ). PHP, Mysql , PHP, Mysql - Stackoverflow.

+5

. .

<?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);
+2

:

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

? 2,0? 42?

, , d ,

, .

, (, ALGOL FORTRAN, ), , .

- .

?

+1

php, :

PHP , . .

, PHP. , , .

: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

MySQL, :

, .

0

, PHP. . .

PHP -

0

- , , C, Fortran .. script , , . PHP .

0

, ( , ). , , (, , 3 -, , :

$_1 = $_REQUEST['1'];

$str1 = $_REQUEST['1'];

, , :

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

This way you can scroll both at the same time:

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

To increase @ hakre's answer, you can also lose quotation marks for brevity

${1}

but alas, you cannot combine this with a string value. I did some experiments, and apparently, PHP expects either a string or a numeric value. And you can do something in curly braces that end up returning a number or a string value or implicitly passed 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" } 

This means that it falseevaluates an empty string, not 0 and trueup to 1.

0
source

All Articles