Variable/object name with dollar sign in it

Hi all,

I have a strange problem here.

I have a script that fetch data from google api with json_decode.


<?php
$result = json_decode(file_get_contents("http://www.google.com/base/feeds/snippets?alt=json&bq=sony"));
print_r($result->feed->title);
?>

If you run the code above it will return the data like below


stdClass Object
(
 [type] => text
 [$t] => Items matching query: sony
)

How do i get the data in $t? (the object name has ‘$’ in it)
PHP gave me “Undefined variable” error.

I know there another way to retrieve the value. I’m just curious, if PHP can support it, then there must be a way to get this value using something like $result->feed->title->$t? But how?

What a curious result!

Anyhow, you can echo the value with following:


echo $result->feed->title->{'$t'};

Looks utterly weird, I know, but it works :slight_smile:

Awesome, it works! Thanks! =)))

You’re welcome :slight_smile:

The use of curly brackets in variables is not very well documented, but they can come in handy sometimes. For example you can also do the following:


$my = 'my';
$myname = 'ScallioXTX';
echo ${ $my . 'name' };

Don’t get carried away though; nine out of ten times when you “need” a solution like the above you’re probably doing something wrong, and the answer is probably to use arrays :slight_smile:
Of course in your case there is no way around it :slight_smile: