$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'