PHP注意:仅在PHP 7上将数组转换为字符串 [英] PHP Notice: Array to string conversion only on PHP 7
本文介绍了PHP注意:仅在PHP 7上将数组转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是PHP的新手.我从php.net
进行了研究,但是今天发现了一个问题.
I am a newbie of PHP. I study it from php.net
, but I found a problem today.
class foo {
var $bar = 'I am bar.';
}
$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo "{$foo->$bar}\n";
echo "{$foo->$baz[1]}\n";
文档( http://php.net/manual/zh/language .types.string.php )表示上面的示例将输出:
The documentation(http://php.net/manual/en/language.types.string.php) say that the above example will output:
I am bar.
I am bar.
但是我在PC(PHP 7)上运行了不同的输出:
But I get the different output run on my PC(PHP 7):
I am bar.
<b>Notice</b>: Array to string conversion in ... on line <b>9</b><br />
<b>Notice</b>: Undefined property: foo::$Array in ... on line <b>9</b><br />
为什么?
推荐答案
这适用于PHP 7:
class foo {
var $bar = 'I am bar.';
}
$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo "{$foo->$bar}\n";
echo "{$foo->{$baz[1]}}\n";
这是因为在PHP 5中出现以下行:
This is caused because in PHP 5 the following line:
echo "{$foo->$baz[1]}\n";
被解释为:
echo "{$foo->{$baz[1]}}\n";
在PHP 7中,它解释为:
While in PHP 7 it's interpreted as:
echo "{{$foo->$baz}[1]}\n";
因此,在PHP 7中,它将整个数组传递给$foo
,而不仅仅是该元素.
And so in PHP 7 it's passing the entire array to $foo
instead of just that element.
这篇关于PHP注意:仅在PHP 7上将数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文