{}(花括号)在PHP字符串字面量中的意义是什么?
这是字符串插值的复杂(卷曲)语法。摘自手册:
Complex (curly) syntax This isn't called complex because the syntax is complex, but because it allows for the use of complex expressions. Any scalar variable, array element or object property with a string representation can be included via this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {. Use {\$ to get a literal {$. Some examples to make it clear: <?php // Show all errors error_reporting(E_ALL); $great = 'fantastic'; // Won't work, outputs: This is { fantastic} echo "This is { $great}"; // Works, outputs: This is fantastic echo "This is {$great}"; echo "This is ${great}"; // Works echo "This square is {$square->width}00 centimeters broad."; // Works, quoted keys only work using the curly brace syntax echo "This works: {$arr['key']}"; // Works echo "This works: {$arr[4][3]}"; // This is wrong for the same reason as $foo[bar] is wrong outside a string. // In other words, it will still work, but only because PHP first looks for a // constant named foo; an error of level E_NOTICE (undefined constant) will be // thrown. 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 "This works too: {$obj->values[3]->name}"; echo "This is the value of the var named $name: {${$name}}"; echo "This is the value of the var named by the return value of getName(): {${getName()}}"; echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}"; // Won't work, outputs: This is the return value of getName(): {getName()} echo "This is the return value of getName(): {getName()}"; ?>
通常,这种语法是不必要的。例如,这个:
$a = 'abcd';
$out = "$a $a"; // "abcd abcd";
行为与此完全相同:
$out = "{$a} {$a}"; // same
所以花括号是不必要的。但这:
$out = "$aefgh";
Will,取决于你的错误级别,要么不工作,要么产生错误,因为没有名为$aefgh的变量,所以你需要做:
$out = "${a}efgh"; // or
$out = "{$a}efgh";
对我来说,花括号可以代替拼接,它们打字更快,代码看起来更干净。记住在PHP解析它们的内容时使用双引号(" "),因为在单引号(' ')中,您将获得提供的变量的字面名称:
<?php
$a = '12345';
// This works:
echo "qwe{$a}rty"; // qwe12345rty, using braces
echo "qwe" . $a . "rty"; // qwe12345rty, concatenation used
// Does not work:
echo 'qwe{$a}rty'; // qwe{$a}rty, single quotes are not parsed
echo "qwe$arty"; // qwe, because $a became $arty, which is undefined
?>
例子:
$number = 4;
print "You have the {$number}th edition book";
//output: "You have the 4th edition book";
如果没有花括号,PHP将尝试寻找一个名为$numberth的变量,但这个变量并不存在!
我还发现访问属性名因迭代器而异的对象属性很有用。例如,我将下面的模式用于一组时间段:小时、日、月。
$periods=array('hour', 'day', 'month');
foreach ($periods as $period)
{
$this->{'value_'.$period}=1;
}
同样的模式也可以用于访问类方法。只需以相同的方式构建方法名,使用字符串和字符串变量。
您可以很容易地主张只使用数组按句点存储值。如果这个应用程序只有PHP,我同意。当类属性映射到数据库表中的字段时,我使用这种模式。虽然可以使用序列化在数据库中存储数组,但如果必须为各个字段建立索引,则效率很低,而且毫无意义。我经常添加一个由字段名组成的数组,由迭代器设置键值,以达到两全其美的目的。
class timevalues
{
// Database table values:
public $value_hour; // maps to values.value_hour
public $value_day; // maps to values.value_day
public $value_month; // maps to values.value_month
public $values=array();
public function __construct()
{
$this->value_hour=0;
$this->value_day=0;
$this->value_month=0;
$this->values=array(
'hour'=>$this->value_hour,
'day'=>$this->value_day,
'month'=>$this->value_month,
);
}
}
推荐文章
- 在c#中解析字符串为日期时间
- 字符串中的单词大写
- 用“+”(数组联合运算符)合并两个数组如何工作?
- Laravel PHP命令未找到
- 如何修复从源代码安装PHP时未发现xml2-config的错误?
- 在PHP中对动态变量名使用大括号
- string.ToLower()和string.ToLowerInvariant()
- 如何从对象数组中通过对象属性找到条目?
- 如何从关联数组中删除键及其值?
- PHP字符串中的花括号
- PHP -如何最好地确定当前调用是否来自CLI或web服务器?
- 无法打开流:没有这样的文件或目录
- 如何删除字符串的第一个和最后一个字符
- 在php中生成一个随机密码
- String, StringBuffer和StringBuilder