php中{}是什么意思
时间:2007-02-17 来源:PHP爱好者
小弟我最近学习php,观摩人家的源代码。由于没有注释,有些不明白,请大虾们帮帮忙,首先在此谢过!
源代码中有些地方{1},{mod}等形式,不知{}是不是解释变量的意思?我翻了一些书也没找到
cnredarrow 回复于:2004-06-02 09:38:16 {程序段的开始
}程序段的结束
xuzuning 回复于:2004-06-02 09:43:48 块
在程序中表示代码块,有$前导时表示数据块
在引号中表示数据块
longnetpro 回复于:2004-06-02 14:30:45 楼上两位答得没错,但楼主并不是这个意思,因此有些答非所问。
{1}或{mod}表示字符串下标。
比如$s{1}表示字符串$s的第2个字节(不是第一个),基本等同于$s[1],只不过后者是老的写法,PHP手册推荐第一种写法,我写的好几个类中都有这种表示方法。
geel 回复于:2004-06-02 14:54:15 综合一下楼上各位
1、
{} 表示程序块的开始和结束,例如
if ($x==$y)
{
do_nothing();
}
2、
{}用来表示字符串下标,例如
(引用longnetpro兄弟的话)
$s{1}表示字符串$s的第2个字节(不是第一个),基本等同于$s[1],只不过后者是老的写法,PHP手册推荐第一种写法
3、
分离变量,例如
$s = "Di, ";
echo ("${s}omething");
//Output: Di, omething
而如果用echo ("$something");
那么就会输出 $something 这个变量。
numlock 回复于:2004-06-03 07:59:39 对于任何更复杂的情况,应该使用复杂语法。
复杂(花括号)语法
不是因为语法复杂而称其为复杂,而是因为用此方法可以包含复杂的表达式。
事实上,用此语法你可以在字符串中包含任何在名字空间的值。仅仅用和在字符串之外同样的方法写一个表达式,然后用 { 和 } 把它包含进来。因为不能转义“{”,此语法仅在 $ 紧跟在 { 后面时被识别(用“{$”或者“{$”来得到一个字面上的“{$”)。用一些例子可以更清晰:
numlock 回复于:2004-06-03 08:00:20 [code:1:7efebf26a1]<?php
// Let's show all errors
error_reporting(E_ALL);
$great = 'fantastic';
// 不行,输出为:This is { fantastic}
echo "This is { $great}";
// 可以,输出为:This is fantastic
echo "This is {$great}";
echo "This is ${great}";
// Works
echo "This square is {$square->width}00 centimeters broad.";
// Works
echo "This works: {$arr[4][3]}";
// This is wrong for the same reason as $foo[bar] is wrong
// outside a string. In otherwords, it will still work but
// because PHP first looks for a constant named foo, it will
// throw an error of level E_NOTICE (undefined constant).
echo "This is wrong: {$arr[foo][3]}";
// Works. When using multi-dimensional arrays, always use
// braces around arrays when inside of strings
echo "This works: {$arr['foo'][3]}";
// Works.
echo "This works: " . $arr['foo'][3];
echo "You can even write {$obj->values[3]->name}";
echo "This is the value of the var named $name: {${$name}}";
?> [/code:1:7efebf26a1]
cnredarrow 回复于:2004-06-03 09:10:58 学习,收获
tom__2004 回复于:2004-06-03 11:44:07 学到东西了!十分感谢各位的帮忙!!!
dualface 回复于:2004-06-04 00:27:41 {} 这样用的说,我还不知道呢。学习学习。
那颗花生 回复于:2005-01-27 22:06:48 现在还看不懂但以后会的
LandyXu 回复于:2005-01-28 16:54:13 学知识了,看来我的基础很是薄弱啊,总结的很详细啊,这里的环境很不错,谢谢各位。
php爱好者站 http://www.phpfans.net 为phper提供一切资讯.
源代码中有些地方{1},{mod}等形式,不知{}是不是解释变量的意思?我翻了一些书也没找到
cnredarrow 回复于:2004-06-02 09:38:16 {程序段的开始
}程序段的结束
xuzuning 回复于:2004-06-02 09:43:48 块
在程序中表示代码块,有$前导时表示数据块
在引号中表示数据块
longnetpro 回复于:2004-06-02 14:30:45 楼上两位答得没错,但楼主并不是这个意思,因此有些答非所问。
{1}或{mod}表示字符串下标。
比如$s{1}表示字符串$s的第2个字节(不是第一个),基本等同于$s[1],只不过后者是老的写法,PHP手册推荐第一种写法,我写的好几个类中都有这种表示方法。
geel 回复于:2004-06-02 14:54:15 综合一下楼上各位
1、
{} 表示程序块的开始和结束,例如
if ($x==$y)
{
do_nothing();
}
2、
{}用来表示字符串下标,例如
(引用longnetpro兄弟的话)
$s{1}表示字符串$s的第2个字节(不是第一个),基本等同于$s[1],只不过后者是老的写法,PHP手册推荐第一种写法
3、
分离变量,例如
$s = "Di, ";
echo ("${s}omething");
//Output: Di, omething
而如果用echo ("$something");
那么就会输出 $something 这个变量。
numlock 回复于:2004-06-03 07:59:39 对于任何更复杂的情况,应该使用复杂语法。
复杂(花括号)语法
不是因为语法复杂而称其为复杂,而是因为用此方法可以包含复杂的表达式。
事实上,用此语法你可以在字符串中包含任何在名字空间的值。仅仅用和在字符串之外同样的方法写一个表达式,然后用 { 和 } 把它包含进来。因为不能转义“{”,此语法仅在 $ 紧跟在 { 后面时被识别(用“{$”或者“{$”来得到一个字面上的“{$”)。用一些例子可以更清晰:
numlock 回复于:2004-06-03 08:00:20 [code:1:7efebf26a1]<?php
// Let's show all errors
error_reporting(E_ALL);
$great = 'fantastic';
// 不行,输出为:This is { fantastic}
echo "This is { $great}";
// 可以,输出为:This is fantastic
echo "This is {$great}";
echo "This is ${great}";
// Works
echo "This square is {$square->width}00 centimeters broad.";
// Works
echo "This works: {$arr[4][3]}";
// This is wrong for the same reason as $foo[bar] is wrong
// outside a string. In otherwords, it will still work but
// because PHP first looks for a constant named foo, it will
// throw an error of level E_NOTICE (undefined constant).
echo "This is wrong: {$arr[foo][3]}";
// Works. When using multi-dimensional arrays, always use
// braces around arrays when inside of strings
echo "This works: {$arr['foo'][3]}";
// Works.
echo "This works: " . $arr['foo'][3];
echo "You can even write {$obj->values[3]->name}";
echo "This is the value of the var named $name: {${$name}}";
?> [/code:1:7efebf26a1]
cnredarrow 回复于:2004-06-03 09:10:58 学习,收获
tom__2004 回复于:2004-06-03 11:44:07 学到东西了!十分感谢各位的帮忙!!!
dualface 回复于:2004-06-04 00:27:41 {} 这样用的说,我还不知道呢。学习学习。
那颗花生 回复于:2005-01-27 22:06:48 现在还看不懂但以后会的
LandyXu 回复于:2005-01-28 16:54:13 学知识了,看来我的基础很是薄弱啊,总结的很详细啊,这里的环境很不错,谢谢各位。
php爱好者站 http://www.phpfans.net 为phper提供一切资讯.
相关阅读 更多 +