如何将值$fbAppPath
放入下面的 PHP 语句中?
<? print json_encode(array(array('text' => 'Become A Fan', 'href' => '$fbAppPath'))); ?>
您无法在单引号字符串中获取变量。PHP 完全按照它们出现的方式解释所有单引号字符串。(除了转义单引号)
使用单引号时获取变量的唯一方法是打破它们:
$foo = 'variable';
echo 'single-quoted-string-'.$foo.'-more-single-quoted-string';
或者
<? print json_encode(array(array('text' => 'Become A Fan', 'href' => "more text ${fbAppPath} more text"))); ?>
如果您想将变量值嵌入到字符串中。在这种情况下,双引号很重要。
<? print json_encode(array(array('text' => 'Become A Fan', 'href' => $fbAppPath))); ?>
您不需要在已经是字符串的变量周围加上引号。
'I am a string, because I am surrounded by quotes';
$string = 'I am a string, because I am surrounded by quotes';
if (is_string($string)) {
echo 'Yes, the variable $string is a string, because it contains a string';
}
$anotherString = $string;
if (is_string($anotherString)) {
echo 'The variable $anotherString is a string as well, because it contains a string as well';
}
$notWhatYouExpect = '$string';
echo $notWhatYouExpect; // outputs the word '$string'