PHP中的可变变量(更多请自行参考php手册):
php中的可变变量说白了,就是拿一个变量的值解析成一个变量名,去读那个变量名的值。实例:
<?<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> ?>
注意:可变变量不能应用于$this和超全局变量 (php变量的作用域和其他高级编程语言有所不同。看代码)
<?<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>?>
变量函数:
<?<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>?>
可变变量的一个典型应用:
<!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>
补充:
变量的特性。如果一个变量事先没有声明,那么如果你要对一个变量赋值,此时PHP在后台的一个操作就是,当你给这个未声明的变量进行赋值时,后台就已经帮你申明了这个变量了。 直接看例子:
<?<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> ?>
小结:看了上面的示例后,你们以后再看到别人封装的代码里,如果有那种没有声明变量就直接用的,不要感到奇怪。那是叫你自己去设置呢。你直接赋值用就行了。。。(PS: 其实本人一开始也感到迷糊,因为以前在搞.NET的在C#里是绝对不会允许这种情况发生的。强型语言用惯了。。。在看这种弱类型的语言,确实一开始不会很适应)