Home  >  Article  >  Backend Development  >  What is a variable variable in php

What is a variable variable in php

(*-*)浩
(*-*)浩Original
2019-09-11 14:37:143243browse

What is a variable variable

What is a variable variable in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn