PHP 语法 $"field" [重复]

Posted

技术标签:

【中文标题】PHP 语法 $"field" [重复]【英文标题】:PHP Syntax $"field" [duplicate]PHP 语法 $"field" [重复] 【发布时间】:2015-06-05 12:01:02 【问题描述】:

我不认识 $"item_$i"; 的 php 语法; item_$i 可以是 item_1 到 item_6。 $ 在作业中表示什么?我在搜索中找不到示例。

if($cl_name=='A') $data = $"item_$i";

感谢您的帮助。

【问题讨论】:

【参考方案1】:

它叫Complex (curly) syntax

这不是因为语法复杂,而是因为它允许使用复杂的表达式。

任何标量变量、数组元素或具有字符串表示的对象属性都可以通过此语法包含在内。只需将表达式写成与它出现在字符串之外的相同方式,然后将其包装在 和 中。由于 无法转义,因此只有在 $ 紧跟 时才能识别此语法。使用 \$ 获取文字 $。一些例子来说明:

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

【讨论】:

以上是关于PHP 语法 $"field" [重复]的主要内容,如果未能解决你的问题,请参考以下文章

基本窗体处理程序

Lucene查询语法详解

php数组序列化a:1:i:0;s:1:"1" 的详细解释

PHP5.4以上更新的伪"多继承"--trait语法

laravel报错,NotFoundHttpException in RouteCollection.php line 161:

Mysql语句 报错 "Unknown column ' ' in 'field list'"