复杂(卷曲)语法
这并不是因为语法复杂,而是因为它允许使用复杂表达式。
任何具有字符串表示的标量变量、数组元素或对象属性都可以通过此语法包括在内。只需将表达式以与字符串外显示的方式相同的方式写入,然后将其包装在{和}..自{无法转义,只有在$立即跟随{..使用{\$得到一个字面上的{$..以下是一些明确的例子:<?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()}";?>