tags:

views:

52

answers:

3
$q = $_GET['q'];

// Load and parse the XML document 

$rss =  simplexml_load_file('http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1');

This returns results containing "$q" instead of results containing the value of $q. Why won't it work?

A: 

Use double quotes instead of single quotes.

animuson
+3  A: 

Change your quotes to double quotes in the simplexml_load_file line

$q = $_GET['q'];

// Load and parse the XML document
$rss = simplexml_load_file("http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1");

PHP will automatically resolve the variable only if the string is in double quotes.

scunliffe
thanks to all. problem solved.
Steven
@Steven: If the question helped you, please accept it.
Felix Kling
+1  A: 

You need to use double quotes to have variables getting expanded:

Variable parsing

When a string is specified in double quotes or with heredoc, variables are parsed within it.

So:

"http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1"

But it would be even better if you use proper URL escaping with urlencode:

'http://search.twitter.com/search.atom?lang=en&q='.urlencode($q).'&rpp=100&page=1'

You can also do that when declaring the $q variable and then use the double quoted string delcaration:

$q = urlencode($_GET['q']);
$rss = simplexml_load_file("http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1");
Gumbo
thanks for the tip
Steven