【发布时间】:2014-09-30 20:58:48
【问题描述】:
这是我的代码:
$jsonData = file_get_contents('http://example.com/bin/serp.php?engine=google&phrase=$name');
它似乎没有正确使用$name。我将如何像我想要做的那样将该变量添加到我的字符串中?
【问题讨论】:
这是我的代码:
$jsonData = file_get_contents('http://example.com/bin/serp.php?engine=google&phrase=$name');
它似乎没有正确使用$name。我将如何像我想要做的那样将该变量添加到我的字符串中?
【问题讨论】:
将单引号改为双引号。 PHP 变量在单引号中时不插值。
$jsonData = file_get_contents("http://example.com/bin/serp.php?engine=google&phrase=$name");
你也可以在这里使用连接:
$jsonData = file_get_contents('http://example.com/bin/serp.php?engine=google&phrase=' . $name);
【讨论】:
将字符串周围的单引号更改为双引号:
"http://example.com/bin/serp.php?engine=google&phrase=$name"
或使用. 运算符进行字符串连接:
'http://example.com/bin/serp.php?engine=google&phrase=' . $name
PHP's Strings documentation 提到了这两种技术。
【讨论】: