首页 > Web开发 > 详细

PHP的大括号(花括号{})使用详解

时间:2016-07-10 21:28:25      阅读:162      评论:0      收藏:0      [点我收藏+]

一、不管什么程序,function name(){}, for(){}, ….这太多了,不说也知道什么用了。

二、$str{4}在字符串的变量的后面跟上{}大括号和中括号[]一样都是把某个字符串变量当成数组处理。

三、{$val}这种情况就是我遇到的问题,这时候大括号起的作用就是,告诉PHP,括起来的要当成变量处理。

如下例子:

//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[fruit]"; // Hello apple

//With one exception, braces surrounding arrays within strings
//allows constants to be looked for
print "Hello {$arr[fruit]}"; // Hello carrot
print "Hello {$arr[‘fruit‘]}"; // Hello apple

 


 

另:PHP 字符串变量中大括号(花括号{})的作用

PHP 变量后面加上一个大括号{},里面填上数字,就是指 PHP 变量相应序号的字符。

例如:

$str = ‘hello‘;

echo $str{0}; // 输出为 h ,也可以 $str[0]
echo $str{1}; // 输出为 e ,也可以 $str[1]

如果要检查某个字符串是否满足多少长度,可以考虑用这种大括号(花括号)加 isset 的方式替代 strlen 函数,因为 isset 是语言结构,strlen 是函数,所以使用 isset 比使用 strlen 效率更高。

比如判断一个字符串的长度是否小于 5:

if  ( !isset ( $str{5} ) ) 就比 if  ( strlen ( $str ) < 5 ) 好。

PHP的大括号(花括号{})使用详解

原文:http://www.cnblogs.com/52php/p/5658314.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!