Number as variable name in php

number as variable name impossible? But it works

${4} = 444; echo ${4}; 

Question: How justified is this syntax? and where is the information about this in the documentation? I have not found.

+4
source share
4 answers

The syntax is described in Variables . No, you are not "justified" in using this syntax. You should absolutely never do this , there is no good reason for using a number as a variable name.

+8
source

Variables between parentheses are considered valid (variable variables), regardless of syntax.

 ${'sad asda sda'} = 444; echo ${'sad asda sda'}; // still works. 
+2
source

it also works

 $_4 = 444; echo $_4; //output 444. 
+1
source

This is an absolutely good json line:

 $json_str = '{"1": "One", "02": "Two"}'; 

So, if I decoded it:

 $json_object = json_decode($json_str); 

way to access items:

 $one = $json_object->{1}; $two = $json_object->{"02"}; 
0
source

Source: https://habr.com/ru/post/1437586/


All Articles