PHP中关联数组的插值(双引号字符串) [英] Interpolation (double quoted string) of Associative Arrays in PHP

查看:84
本文介绍了PHP中关联数组的插值(双引号字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内插PHP的字符串索引数组元素(5.3.3,Win32)时 可能不会出现以下现象:

When interpolating PHP's string-indexed array elements (5.3.3, Win32) the following behavior may be expected or not:

$ha = array('key1' => 'Hello to me');

print $ha['key1'];   # correct (usual way)
print $ha[key1];     # Warning, works (use of undefined constant)

print "He said {$ha['key1']}"; # correct (usual way)
print "He said {$ha[key1]}";   # Warning, works (use of undefined constant)

print "He said $ha['key1']";   # Error, unexpected T_ENCAPSED_AND_WHITESPACE
print "He said $ha[ key1 ]";   # Error, unexpected T_ENCAPSED_AND_WHITESPACE
print "He said $ha[key1]";     # !! correct (How Comes?)

有趣的是,最后一行似乎是正确的PHP代码.有什么解释吗? 可以信任此功能吗?

Inerestingly, the last line seems to be correct PHP code. Any explanations? Can this feature be trusted?


现在发布的要点设置在粗体中,以减少误解.


The point of the posting now set in bold face in order to reduce misunderstandings.

推荐答案

是的,您可以信任它. 涵盖了所有变量插值方式文档很好.

Yes, you may trust it. All ways of interpolation a variable are covered in the documentation pretty well.

如果您想知道这样做的原因,那么,我不能为您提供帮助.但还是一如既往:PHP很老,并且发展了很多,因此引入了不一致的语法.

If you want to have a reason why this was done so, well, I can't help you there. But as always: PHP is old and has evolved a lot, thus introducing inconsistent syntax.

这篇关于PHP中关联数组的插值(双引号字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆