tags:

views:

272

answers:

5

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

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

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

konforce
+3  A: 

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.

jodonnell
A: 

Ok, I figured it out... I didn't realize I was in a function and referencing a variable outside the function... sorry to waste anyone's time

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

variable being referenced was in the global scope..

Mark
if this is the correct solution to the problem, you can accept your own answer
Jonathan Fingland
@Mark, nevertheless you should give proper credit to those who helped you and, in my opinion, you should accept one of the perfect 3 valid answers given. That will give points both ways.
Frankie
In two days. Although really it isn't the answer to the problem, as it doesn't relate to the problem.
Chacha102
@Jonathan, actually I believe he can accept his own answer but only a couple of days after it's been posted.
Frankie
@Frankie thanks. I've never tried to accept my own so that's never come up. good to know, though.
Jonathan Fingland
Yeah, it was a stupid mistake... It's late... The most accurate and succinct answer was the first one by konforce... had the variable been in the same scope as the statement his(her) answer would have resolved the issue. As such, that's the answer I will accept...
Mark
+6  A: 

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';
Chacha102
A: 

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