-3

Here's my code:

$jsonData = file_get_contents('http://example.com/bin/serp.php?engine=google&phrase=$name');

It doesn't appear to be using $name correctly. How would I add that variable into my string like I'm trying to do?

John Conde
  • 212,985
  • 98
  • 444
  • 485

2 Answers2

6

Change the single quotes to double quotes. PHP variables are not interpolated when in single quotes.

$jsonData = file_get_contents("http://example.com/bin/serp.php?engine=google&phrase=$name");

You can also use concatenation here:

$jsonData = file_get_contents('http://example.com/bin/serp.php?engine=google&phrase=' . $name);
John Conde
  • 212,985
  • 98
  • 444
  • 485
3

Either change the single quotes around your string to double quotes:

"http://example.com/bin/serp.php?engine=google&phrase=$name"

Or use string concatenation with the . operator:

'http://example.com/bin/serp.php?engine=google&phrase=' . $name

Both of these techniques are mentioned on PHP's Strings documentation.

James Donnelly
  • 122,518
  • 33
  • 200
  • 204