PHP语法${“字段”}

PHP语法${“字段”},php,string,variables,Php,String,Variables,我不认识${“item_$I”}的PHP语法;项目_$i可以是项目_1到项目_6。${}在赋值中表示什么?我在搜索中找不到示例 if($cl_name=='A') $data = ${"item_$i"}; 谢谢你的帮助。它叫 之所以称之为complex,不是因为语法复杂,而是因为它允许使用复杂表达式 任何标量变量、数组元素或具有字符串表示的对象属性都可以通过此语法包含在内。只需按照在字符串外部显示的方式编写表达式,然后用{和}将其包装。由于{不能被转义,因此只有当$紧跟在{后面时,才会识别此

我不认识${“item_$I”}的PHP语法;项目_$i可以是项目_1到项目_6。${}在赋值中表示什么?我在搜索中找不到示例

if($cl_name=='A') $data = ${"item_$i"};
谢谢你的帮助。

它叫

之所以称之为complex,不是因为语法复杂,而是因为它允许使用复杂表达式

任何标量变量、数组元素或具有字符串表示的对象属性都可以通过此语法包含在内。只需按照在字符串外部显示的方式编写表达式,然后用{和}将其包装。由于{不能被转义,因此只有当$紧跟在{后面时,才会识别此语法。使用{\$获得一个文本{$。下面的一些示例说明:


<?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()}";
?>