开发者

How do I get a PHP variable between single quotes?

How can I get the value $fbAppPath into the PHP statement below?

<? print json_encode(array(array('text' => 'Become A Fan', 'href' => '$fbAppPath'))); ?>开发者_开发百科;


You cannot get a variable in a single quote string. PHP interprets all single-quoted strings EXACTLY as they appear. (aside from escaping a single quote)

The ONLY way to get a variable while using single quotes is to break out of them:

$foo = 'variable';
echo 'single-quoted-string-'.$foo.'-more-single-quoted-string';


Or

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

if you wanted to embed the variable value in a string. The double quotes are important in that case.


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


You do not need quotes around a variable that is already a string.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜