[英]Quotes inside Quotes inside another Quotes
<input <?php echo $credit['credit_1']=='0'?'placeholder="Credit 1"':'value="$credit['credit_1']"';?> >
我的问题是,哪一个是正确的,为什么?
1. 'value="$credit_exm['credit_1']"'
2. 'value="$credit_exm["credit_1"]"'
回答:都没有。
从手册:
注意:与双引号和heredoc 语法不同,特殊字符的变量和转义序列在出现在单引号字符串中时不会被扩展。
以下是获得相同输出的不同方法:
<?php
$my['planet'] = 'Earth!';
$strings = [];
array_push(
$strings,
'Hello "' . $my['planet'] . '"',
"Hello \"${my['planet']}\"",
"Hello \"{$my['planet']}\"",
"Hello \"$my[planet]\"",
sprintf('Hello "%s"', $my['planet'])
);
var_export($strings);
输出:
array (
0 => 'Hello "Earth!"',
1 => 'Hello "Earth!"',
2 => 'Hello "Earth!"',
3 => 'Hello "Earth!"',
4 => 'Hello "Earth!"',
)
我个人发现这样做时我很容易陷入困境,所以经常选择 sprintf 方法。
语法错误。
必须:
<input <?php echo $credit['credit_1'] == '0' ? 'placeholder="Credit 1"' : 'value="' . $credit["credit_1"] . '"'; ?> >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.