2

如何将值$fbAppPath放入下面的 PHP 语句中?

<? print json_encode(array(array('text' => 'Become A Fan', 'href' => '$fbAppPath'))); ?>
4

4 回答 4

8

您无法在单引号字符串中获取变量。PHP 完全按照它们出现的方式解释所有单引号字符串。(除了转义单引号)

使用单引号时获取变量的唯一方法是打破它们:

$foo = 'variable';
echo 'single-quoted-string-'.$foo.'-more-single-quoted-string';
于 2010-02-04T02:05:33.047 回答
4

或者

<? print json_encode(array(array('text' => 'Become A Fan', 'href' => "more text ${fbAppPath} more text"))); ?>

如果您想将变量值嵌入到字符串中。在这种情况下,双引号很重要。

于 2010-02-04T01:58:59.130 回答
3

<? print json_encode(array(array('text' => 'Become A Fan', 'href' => $fbAppPath))); ?>

于 2010-02-04T01:58:04.860 回答
-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'
于 2010-02-04T02:10:26.233 回答