-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
  • 217,595
  • 99
  • 455
  • 496

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
  • 217,595
  • 99
  • 455
  • 496
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
  • 126,410
  • 34
  • 208
  • 218