Home > Article > Backend Development > What is a variable variable in php
What is a variable variable
I don’t know if you have encountered this when using php One situation is when you want to use the contents of one variable as the name of another variable. In php, this requirement can be achieved through variable variables (Variable variables).
The general form of variable variables is: (Recommended learning: PHP programming from entry to proficiency)
$var=“foo”; $$var=1;
Here, it is actually equivalent to expanding var, and then using its value as the real variable name
$foo=1;
The magical use of variable variables
Dynamic instantiation of class
$var=“foo”; $a=new $foo;
Loop definition of variables
for($i=0;$i<10;$i++){ ${aa.$i}=“a”; }
Dynamic calling method
class test_class{ var $func=‘display_UK’; function display_UK(){ echo “Hello”; } function display_FR(){ echo “Bonjour”; } function display(){ $this->{$this->func}(); } }
The above is the detailed content of What is a variable variable in php. For more information, please follow other related articles on the PHP Chinese website!