Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
双引号字符串中的php变量解析_Php_String - Fatal编程技术网

双引号字符串中的php变量解析

双引号字符串中的php变量解析,php,string,Php,String,在阅读php手册中关于双引号字符串中变量解析的内容时,我遇到了两个让我感到困惑的示例。举一个例子,说明这如何与一些代码一起工作,会有很大帮助。以下是手册中的代码: 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 ret

在阅读php手册中关于双引号字符串中变量解析的内容时,我遇到了两个让我感到困惑的示例。举一个例子,说明这如何与一些代码一起工作,会有很大帮助。以下是手册中的代码:

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

这到底是什么意思?我知道,
$obj
是一个对象。我只是不知道这些例子的前导代码是什么。任何帮助都会很有用。

以下是您发布的示例

使用
{}
时,确保对对象的整个路径进行求值。如果删除它们,它将只计算
$obj->values
,返回
数组

$obj->values[3] = "Name";
echo "This works too: {$obj->values[3]}"."\n"; // CORRECT: This works too: Name
echo "This works too: $obj->values[3]"."\n\n"; // ERROR:   This works too: Array[3]
在前两个示例中,首先计算
$name
,剩下的是{$mike}。由于新变量名外有大括号,因此也将对其进行计算,并将转换为字符串
Michael
。在最后一个示例中,您将看到
$mike

$name = "mike"; 
$mike = "Michael"; 
echo "This is the value of the var named $name: {${$name}}"."\n"; // This is the value of the var named mike: Michael
echo "This is the value of the var named $name: {$$name}"."\n";   // This is the value of the var named mike: Michael
echo "This is the value of the var named $name: $$name"."\n\n";   // This is the value of the var named mike: $mike
此示例与上面的示例类似,但不使用变量$name,而是使用函数。如果在函数(例如#2)周围没有大括号,但在函数$getName()周围没有大括号,PHP将尝试访问
函数$getName
,该函数不是合法的函数名。 最后一个示例$getName()将获取变量$getName的值,而()将不受影响。因此,如果您想要
$getName=“Heya”,它将成为
Heya()


让我们使用您提供的手册中的3个示例

我们可以为这些变量提供一个名称,并查看输出以了解它们的作用

$obj->values[3]->name = "Object Value";
$name = "variable";
$variable = "Variable Value"; // You'll see why we need to define this in a minute
$function = "Function Value";
function getName() {
    return "function";
}
我想你已经看到了这一点,但让我们看看这对你发布的声明意味着什么:

echo "This works too: {$obj->values[3]->name}"; // This works too: Object Value

echo "This is the value of the var named $name: {${$name}}"; // This is the value of the var named $name: Variable Value

echo "This is the value of the var named by the return value of getName(): {${getName()}}"; // This is the value of the var named by the return value of getName(): Function Value
在第一种情况下,它将对象值替换为“对象值”

在第二个
$name
被解释为“变量”,这意味着
{${$name}}
被解释为
$variable
的值,即“变量值”


同样的原则也适用于函数的返回值。

可能重复@Dagon,我认为这个问题只需要重新命名。。。似乎内容更多的是关于如何解释花括号内的变量(在双引号内),等等@jerdigty花括号在链接的duplicate@bboy然后搜索php OO的基础知识只要阅读手册中的这篇文章,经过一些思考,它使事情变得更好:
注意:函数、方法调用、,{$}中的静态类变量和类常量从PHP5开始工作。但是,访问的值将被解释为定义字符串的范围内的变量名称。使用单大括号({})无法访问函数或方法的返回值或类常量或静态类变量的值。
echo "This works too: {$obj->values[3]->name}"; // This works too: Object Value

echo "This is the value of the var named $name: {${$name}}"; // This is the value of the var named $name: Variable Value

echo "This is the value of the var named by the return value of getName(): {${getName()}}"; // This is the value of the var named by the return value of getName(): Function Value