admin 发表于 2011-6-24 17:34:41

PHP变量使用大括号的异同

前一篇日志提到的PHP Notice警告导致Ajax请求失效的情况。深究其原因,还是在13行的switch语句上。因此,我做了一些关于PHP变量使用大括号的测试,也发现了一下使用中需注意的地方。


一、使用大括号的变量
前一篇日志提到了,PHP Notice警告的是下面一句:

switch (${action}.'_'.${child}) {

初看并没有什么问题。我也查询了PHP手册上关于变量的定义:这里。
1、可变变量的情况
可见,与大部分资料一样,变量使用大括号的情况,在于“可变变量”(Variable variables)。其中提到:
引用
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a then the parser needs to know if you meant to use $a as a variable, or if you wanted $$a as the variable and then the index from that variable. The syntax for resolving this ambiguity is: ${$a} for the first case and ${$a} for the second.


也就是说,为了在数组环境中也可以使用可变变量,因此,需要根据不同的情况,恰当的使用大括号{}限制变量的范围。${$a} 与${$a} 是完全不同的:
引用
${$a}  这里$a是一个变量;
${$a} 这里$a是一个变量;


2、定界、避免歧义
实际上,这情况与可变变量时类似。例如,若使用“.”连接符,连接一个字符串,可能是这样:

echo $str.'_2010';

用大括号来写,可能更简单:

echo "${str}_2010";

可见,如果没有大括号,则可能把$str_2010整个作为一个变量来处理。当然,这样的写法,只能用在双引号中,单引号里面是不会执行变量替换的。

3、字符串变量中的单个字符
例如:

<?php
$str='000';
$str{0}='1';
echo $str; //输出为100
?>

这不难理解,与中括号[]的作用是一致的,有点类似Python中把字符串看成对象的情况。所以,下面的语句功能相同:

<?php
$str='000';
$str='1';
echo $str; //也是输出100
?>

不过,这些都不是我想说明的内容,真正想描述的情况,请见下面。

二、变量使用大括号的异同
首先,把PHP的错误信息输出全部打开,即/etc/php.ini 为:
引用
error_reporting  =  E_ALL
display_errors = On


然后,打开测试页面,其中代码为:

<?php
$test='123';
echo $test;
echo "${test}";
echo "{$test}";
echo ${test}.'_';
echo ${test};
?>

你会看到如下的结果:
引用
123123123
Notice: Use of undefined constant test - assumed 'test' in /var/www/html/phpcrm/testpages/variables.php on line 6
123_
Notice: Use of undefined constant test - assumed 'test' in /var/www/html/phpcrm/testpages/variables.php on line 7
123


这说明什么?
1、可接受的写法
从输出结果中“123123123”,表明前面三行的echo语句都是正常的:

echo $test;
echo "${test}";
echo "{$test}";

2、不建议的写法
后面的两行都有Notice警告,也就是曾把test变量看成常量,只是后来才假设为变量来处理的。因此,为了避免歧义和冲突,不建议这样写:

echo ${test}.'_';
echo ${test};

3、不正确的写法
网上不少资料介绍,${var}与{$var}的作用是一样的。但是,如果你再加入一句:

echo {$test};

那么,你将会得到以下错误信息:
引用
Parse error: syntax error, unexpected '{' in /var/www/html/phpcrm/testpages/variables.php on line 8


这可不是Notice警告,是错误,因解析问题,程序将不能正常运行。

三、总结
结合前面两部分的内容,我相信,对于变量引用时使用大括号,应遵循以下原则:
引用
1、正确的写法为:${var} 的形式;
2、与双引号一同使用;
3、根据需表达的意思进行定界。


所以,最后我把switch一行改为:

switch ("${action}_${child}") {

即不再出现Notice警告。





如:
$sql = "insert into article (`channel_id`,`title`,`detail`,`pub_time`) values ('{$cid}','{$title}','{$detail}','{$time}');";

不加似乎也可以,加{}是什么意思呢?
还有字段名 为什么要以``包括呢? ==================================================至少便于阅读嘛~~~''是insert into语句要求的,因为字符串要成对出现嘛
加{}有时候是为了防止变量名和后面的字符串连在一起嘛
例如
{$cid}dd
如果cid=aa
那么{$cid}dd=aadd
不加的话你自己看看了$ciddd,岂不变成了ciddd变量了~~

//   The   following   is   okay   as   it's   inside   a   string.     Constants   are   not           //   looked   for   within   strings   so   no   E_NOTICE   error   here           print   "Hello   $arr";             //   Hello   apple                     //   With   one   exception,   braces   surrounding   arrays   within   strings           //   allows   constants   to   be   looked   for           print   "Hello   {$arr}";         //   Hello   carrot         [*]  print   "Hello   {$arr['fruit']}";     //   Hello   apple   
PHP 变量后面加上一个大括号{},里面填上数字,就是指 PHP 变量相应序号的字符。
例如:
$str = 'hello';
echo $str{0}; // 输出为 h
echo $str{1}; // 输出为 e
如果要检查某个字符串是否满足多少长度,可以考虑用这种大括号(花括号)加 isset 的方式替代 strlen 函数,因为 isset 是语言结构,strlen 是函数,所以使用 isset 比使用 strlen 效率更高。
比如判断一个字符串的长度是否小于 5:
if ( !isset ( $str{5} ) ) 就比 if ( strlen ( $str ) < 5 ) 好。
下面几个比较能说明原因的解释是:
表示{}里面的是一个变量  ,执行时按照变量来处理     [*]在字符串中引用变量使用的特殊包括方式,这样就可以不使用.运算符,从而减少代码的输入量了。
其实输出那块是等同于print   "hello   ".$arr['fruit'];


页: [1]
查看完整版本: PHP变量使用大括号的异同