Home > Backend Development > PHP Problem > What is a variable variable in php

What is a variable variable in php

(*-*)浩
Release: 2023-02-23 22:20:01
Original
3374 people have browsed it

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;
Copy after login

Here, it is actually equivalent to expanding var, and then using its value as the real variable name

$foo=1;
Copy after login

The magical use of variable variables

Dynamic instantiation of class

$var=“foo”;
$a=new $foo;
Copy after login

Loop definition of variables

for($i=0;$i<10;$i++){
${aa.$i}=“a”;
}
Copy after login

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}();
  }
}
Copy after login

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!

Related labels:
php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template