Home >Backend Development >PHP Tutorial >PHP study notes--about variables in php_PHP tutorial
Variable variables in PHP (please refer to the PHP manual for more information):
To put it bluntly, variable variables in php are to parse the value of a variable into a variable name and read the value of that variable name. Example:
<?<span php </span><span $a</span> = "China"; <span //</span><span 变量a</span> <span $b</span> = "a"; <span //</span><span 变量b</span> <span $China</span> = "I'm Chinese !"; <span //</span><span 变量China</span> <span $f</span> = "b"; <span //</span><span 变量f</span> <span echo</span> <span $a</span>."<br />"; <span //</span><span 输出 China</span> <span echo</span> $<span $a</span>."<br />"; <span //</span><span 输出 I'm Chinese --这里像要当做可变变量解析,必须在前面多加一个$符号</span> <span $a</span> = "f"; <span //</span><span 改变变量指向的名称(这里就是可变变量的应用)</span> <span echo</span> $<span $a</span>."<br />"; <span //</span><span 经过上面指向变量f后输出 b</span> <span $a</span> = "b"; <span //</span><span 同上</span> <span echo</span> $<span $a</span>."<br /><br />"; <span //</span><span 输出 a</span> <span echo</span> <span $b</span>."<br />"; <span //</span><span 输出 a</span> <span echo</span> $<span $b</span>."<br />"; <span //</span><span 输出 b</span> <span echo</span> $$<span $b</span>."<br /><br />"; <span //</span><span 输出 a</span> <span echo</span> <span $f</span>."<br />"; <span //</span><span 输出 b</span> <span echo</span> $<span $f</span>."<br />"; <span //</span><span 输出 a</span> <span echo</span> $$<span $f</span>."<br />"; <span //</span><span 输出 b</span> <span echo</span> $$$<span $f</span>."<br /><br />"; <span //</span><span 输出 a</span> <span $</span><span $a</span> = "China"; <span //</span><span 前面最后一个已经更改了变量为b所谓$$a=$b 也就是改变的$b的值</span> <span echo</span> <span $b</span>."<br />"; <span //</span><span 输出 China</span> <span echo</span> $<span $b</span>; <span //</span><span 输出 I'm Chinese</span> ?>
Note: Mutable variables cannot be applied to $this and superglobal variables (the scope of PHP variables is different from other high-level programming languages. See the code)
<?<span php </span><span $name</span> = 'man'<span ; $</span><span $name</span> = 'abc'; <span //</span><span 如果事先没有man这个变量。就新建一个man变量。 然后把abc赋值过去</span> $$<span $name</span> = 'def'<span ; </span><span echo</span> <span $man</span>."<br />"; <span //</span><span 输出abc</span> <span echo</span> <span $abc</span>; <span //</span><span 输出def</span> <span echo</span> "<br /> <hr />"<span ; </span><span function</span><span show() { </span><span global</span> <span $name</span>; <span //</span><span 这里的global并不是设置为全局变量。而是引用</span> <span echo</span> <span $name</span>."<br />"; <span //</span><span 输出man</span> <span } </span><span function</span><span showtwo() { </span><span //</span><span global $name; //echo $name."<br />";</span> <span echo</span> <span $GLOBALS</span>['name']; <span //</span><span 超全局变量数组</span> <span } show(); showtwo(); </span>?>
Variable function:
<?<span php </span><span function</span><span b() { </span><span echo</span> "这是B"<span ; } </span><span function</span> c(<span $name</span> = "China") <span //</span><span 设默认值</span> <span { </span><span echo</span> "这是<span $name</span>"<span ; } </span><span $a</span> = 'b'<span ; </span><span $a</span>(); <span //</span><span 找值所在的函数</span> <span $a</span> = 'c'<span ; </span><span $a</span><span ();<br /> </span>?>
A typical application of variable variables:
<!DOCTYPE html <span PUBLIC</span> "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <div> <form action="#" method="post"> <label>name: </label> <input type="text" name="name" /><br /> <label>pwd : </label> <input type="text" name="pwd" /><br /> <label>tag : </label> <input type="text" name="tag" /><br /> <input type="submit" value="提交" /> </form> </div> <?<span php </span><span foreach</span>(<span $_POST</span> <span as</span> <span $key</span>=><span $value</span><span ) { </span><span //</span><span print_r($_POST);</span> $<span $key</span> = <span $value</span><span ; } </span><span //</span><span extract($_POST); //从数组中将变量导入到当前的符号表 --自行查找php手册</span> <span echo</span> <span $name</span>."<br />"<span ; </span><span echo</span> <span $pwd</span>."<br />"<span ; </span><span echo</span> <span $tag</span>."<br />"<span ; </span>?> </body> </html>
Supplement:
Characteristics of variables. If a variable has not been declared in advance, then if you want to assign a value to a variable, one of the operations of PHP in the background is that when you assign a value to the undeclared variable, the background has already declared the variable for you. Just look at the example:
<?<span php </span><span class</span><span A { </span><span public</span> <span function</span><span show() { </span><span //</span><span 记住这里的name 实现是没有声明的。</span> <span echo</span> (<span isset</span>(<span $this</span>->name)?"true":"false")." -- "<span ; </span><span echo</span> <span $this</span>-><span name; } } </span><span $A</span> = <span new</span> A(); <span //</span><span 实例化 //直接输出,是没有任何结果的. 因为没有这个变量 。。这里可以用isset判断为false</span> <span $A</span>->show(); <span //</span><span 输出 "fase -- " //这里进行赋值,在赋值时,后台默认声明此变量</span> <span $A</span>->name = "我有输出了。这个变量被声明了!"<span ; </span><span echo</span> "<br />"<span ; </span><span $A</span>->show(); <span //</span><span 输出 "true -- 我有输出了。这个变量被声明了!"</span> ?>
Summary: After reading the above example, don’t be surprised if you see other people’s encapsulated code in the future and use it directly without declaring variables. That means you have to set it up yourself. You can just assign it directly. . . (PS: In fact, I was confused at first, because those who used to work on .NET would never allow this to happen in C#. I am used to using strong-type languages... When I look at this weak-type language, It’s true that you won’t get used to it very well at first)