如何将值$fbAppPath
放入下面的PHP语句中?
<? print json_encode(array(array('text' => 'Become A Fan', 'href' => '$fbAppPath'))); ?>
答案 0 :(得分:8)
您无法在单引号字符串中获取变量。 PHP会在出现时完全解释所有单引号字符串。 (除了逃避单引号)
使用单引号获取变量的唯一方法是打破它们:
$foo = 'variable';
echo 'single-quoted-string-'.$foo.'-more-single-quoted-string';
答案 1 :(得分:4)
或
<? print json_encode(array(array('text' => 'Become A Fan', 'href' => "more text ${fbAppPath} more text"))); ?>
如果要将变量值嵌入字符串中。在这种情况下,双引号很重要。
答案 2 :(得分:3)
<? print json_encode(array(array('text' => 'Become A Fan', 'href' => $fbAppPath))); ?>
答案 3 :(得分:-2)
您不需要围绕已经是字符串的变量的引号。
'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'