Why isn't my variable expanded in this string?
$q = $_GET['q'];
// Load and parse the XML document
$rss = simplexml_load_file('http://sea开发者_如何学Gorch.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?
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.
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");
Use double quotes instead of single quotes.
精彩评论