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 に答える