php PHP中关联数组的插值(双引号字符串)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4738850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Interpolation (double quoted string) of Associative Arrays in PHP
提问by rubber boots
When interpolating PHP's string-indexed array elements (5.3.3, Win32) the following behavior may be expected or not:
插入 PHP 的字符串索引数组元素(5.3.3,Win32)时,可能会出现以下行为,也可能不会:
$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?)
Inerestingly, the last line seems to be correct PHP code. Any explanations? Can this feature be trusted?
有趣的是,最后一行似乎是正确的 PHP 代码。有什么解释吗?这个功能可以信任吗?
编辑:现在发帖的重点是 bold face大胆面对,以减少误解。
回答by NikiC
Yes, you may trust it. All ways of interpolation a variable are covered in the documentationpretty well.
是的,你可以相信它。文档中很好地涵盖了对变量进行插值的所有方法。
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 很旧并且已经发展了很多,因此引入了不一致的语法。
回答by mfonda
Yes, this is well defined behavior, and will always look for the string key 'key'
, and not the value of the (potentially undefined) constant key
.
是的,这是定义明确的行为,并且将始终查找字符串 key 'key'
,而不是(可能未定义的)常量的值key
。
For example, consider the following code:
例如,考虑以下代码:
$arr = array('key' => 'val');
define('key', 'defined constant');
echo "$arr[key] within string is: $arr[key]";
This will output the following:
这将输出以下内容:
$arr[key] within string is: val
That said, it's probably not best practice to write code like this, and instead either use:
也就是说,编写这样的代码可能不是最佳实践,而是使用:
$string = "foo {$arr['key']}"
or
或者
$string = 'foo ' . $arr['key']
syntax.
句法。
回答by mario
The last one is a special case handled by the PHP tokenizer. It does not look up if any constant by that name was defined, it always assumes a string literal for compatibility with PHP3 and PHP4.
最后一个是由 PHP 标记器处理的特殊情况。它不会查找是否定义了该名称的任何常量,它始终假定字符串文字以与 PHP3 和 PHP4 兼容。
回答by aqm
To answer your question, yes, yes it can, and much like implode and explode, php is very very forgiving... so inconsistency abound
回答你的问题,是的,是的,它可以,就像内爆和爆炸一样,php非常宽容......所以不一致比比皆是
And I have to say I like PHP's interpolation for basical daisy punching variables into strings then and there,
我不得不说我喜欢 PHP 的插值,用于将基本雏菊变量插入字符串,然后和那里,
However if your doing only string variable interpolation using a single array's objects, it may be easier to write a template which you can daisy print a specific object variables into (like in say javascript or python) and hence explicit control over the variable scope and object being applied to the string
但是,如果您只使用单个数组的对象进行字符串变量插值,则编写一个模板可能会更容易,您可以将特定对象变量打印到其中(例如在 javascript 或 python 中),从而显式控制变量范围和对象应用于字符串
I though this guy's isprintf really useful for this kind of thing
我虽然这家伙的 isprintf 对这种事情真的很有用
http://www.frenck.nl/2013/06/string-interpolation-in-php.html
http://www.frenck.nl/2013/06/string-interpolation-in-php.html
<?php
$values = array(
'who' => 'me honey and me',
'where' => 'Underneath the mango tree',
'what' => 'moon',
);
echo isprintf('%(where)s, %(who)s can watch for the %(what)s', $values);
// Outputs: Underneath the mango tree, me honey and me can watch for the moon