可变变量的样例
1 <?php 2 class foo { 3 var $bar = 'I am bar.'; 4 var $arr = array('I am A.', 'I am B.', 'I am C.'); 5 var $r = 'I am r.'; 6 } 7 8 $foo = new foo(); 9 $bar = 'bar'; 10 $baz = array('foo', 'bar', 'baz', 'quux'); 11 echo $foo->$bar . " "; 12 echo $foo->$baz[1] . " "; 13 14 $start = 'b'; 15 $end = 'ar'; 16 echo $foo->{$start . $end} . " "; 17 18 $arr = 'arr'; 19 echo $foo->$arr[1] . " "; 20 echo $foo->{$arr}[1] . " "; 21 22 ?>
最后显示为
1 I am bar. 2 I am bar. 3 I am bar. 4 I am r. 5 I am B.
单引号&&双引号
单引号:不像双引号和 heredoc 语法结构,在单引号字符串中的变量和特殊字符的转义序列将不会被替换。
样例为
1 <?php 2 3 $s = "ideone"; 4 $s1 = '\Hello World $s'; //输出为Hello World $s 5 $s2 = "\Hello World && $s"; //输出为Hello World && ideone 6 echo $s1; 7 echo $s2; 8 9 //另外在类似一下的字符串自我嵌套中嵌套的部分是不会迭代的而是空 10 $s3 = "Hello World $s3"; //输出为Hello World 11 echo $s3; 12 ?>