栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

$ {}在PHP语法中是什么意思?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

$ {}在PHP语法中是什么意思?

${ }
(美元大括号)被称为
复杂(卷曲)语法


之所以称其为“复杂”,是因为语法复杂,而是因为它允许使用复杂的表达式。

可以通过此语法包括具有字符串表示形式的任何标量变量,数组元素或对象属性。只需以与出现在字符串外部相同的方式编写表达式,然后将其包装在

{
和中即可
}
。由于
{
无法转义,因此仅在
$
紧随其后的才会识别此语法
{
。使用
{$
得到文字
{$
。一些示例可以使您清楚:

<?php// Show all errorserror_reporting(E_ALL);$great = 'fantastic';// Won't work, outputs: This is { fantastic}echo "This is { $great}";// Works, outputs: This is fantasticecho "This is {$great}";echo "This is ${great}";// Worksecho "This square is {$square->width}00 centimeters broad.";// Works, quoted keys only work using the curly brace syntaxecho "This works: {$arr['key']}";// Worksecho "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 stringsecho "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()}”;
?>



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/417072.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号