问题描述
我知道这是一个非常基本的问题,但是我不得不问.
I know It's a very basic question but I have to ask.
我有一个关联数组,假设它是:
I have an associative array let's say it is:
$couple = array('husband' => 'Brad', 'wife' => 'Angelina');
现在,我要在字符串中打印丈夫姓名.有很多方法,但是我想这样做,但它会产生html错误
Now, I want to print husband name in a string. There are so many ways but i want to do this way but it gives html error
$string = "$couple[\'husband\'] : $couple[\'wife\'] is my wife.";
如果我对反斜杠使用了错误的语法,请更正我.
Please correct me if I'm using a wrong syntax for backslash.
推荐答案
您的语法正确.
但是,相对于双引号,您仍然更喜欢单引号.
But, still you can prefer single quotes versus double quotes.
因为变量插值,双引号会变慢.
Because, double quotes are a bit slower due to variable interpolation.
(解析双引号中的变量,单引号则不然.)
(variables within double quotes are parsed, not the case for single quotes.)
代码的更优化和更干净的版本:
A more optimized and cleaned version of your code:
$string = $couple['husband'] .' : ' . $couple['wife'] .' is my wife.';
这篇关于如何在字符串中回显关联数组的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!