Home >Backend Development >PHP Tutorial >How to use the countif function How to use variable variable names in PHP

How to use the countif function How to use variable variable names in PHP

WBOY
WBOYOriginal
2016-07-29 08:47:531549browse

Usually variables are named by statements like the following:

Copy code The code is as follows:


$a = 'hello';
?>


The variable variable name refers to Use the value of a variable as the name of the variable. In the example above, you can set hello to the name of a variable by using two $ signs, like below.

Copy code The code is as follows:


$$a = 'world';
?>


Through the above two statements, two variables are defined: variable $ a, the installed content is "hello" and the variable $hello, the installed content is "world". So, the following language:

Copy code The code is as follows:


echo "$a ${$a}";
?>


is exactly the same as the output of the following statement Consistent:

Copy the code The code is as follows:


echo "$a $hello";
?>


They all output: hello world.
In order to use mutable variable names for arrays, you need to resolve an ambiguity problem. That is, if you write $$a[1], the parser needs to understand whether you mean to treat $a[1] as a variable, or to treat $$a as a variable. [1] refers to this variable. index. The syntax to resolve this ambiguity is: use ${$a[1]} in the first case and ${$a}[1] in the second case.
Class properties can also be accessed through mutable property names. Mutable property names are taken from the access scope of the variable in which the call was made. For example, if your expression is like this: $foo->$bar, then the runtime will look for the variable $bar in the local variable scope, and its value will be used as a property name of the $foo object. It can also be used if $bar is an array.
Example 1 Variable variable name

Copy the code The code is as follows:


class foo {
var $bar = 'I am bar.';
}
$foo = new foo( );
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "n";
echo $ foo->$baz[1] . "n";
?>


The above example will output the following results:
I am bar.
I am bar.
Warning
Please note that mutable variables Names cannot be used on super global array variables in PHP functions and classes. The variable $this is also a special variable that cannot be dynamically named.

The above introduces how to use the countif function and how to use variable variable names in PHP, including how to use the countif function. I hope it will be helpful to friends who are interested in PHP tutorials.

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