【问题标题】:How to enter variable into string? [duplicate]如何将变量输入字符串? [复制]
【发布时间】:2014-09-30 20:58:48
【问题描述】:

这是我的代码:

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

它似乎没有正确使用$name。我将如何像我想要做的那样将该变量添加到我的字符串中?

【问题讨论】:

    标签: php string variables


    【解决方案1】:

    将单引号改为双引号。 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);
    

    【讨论】:

      【解决方案2】:

      将字符串周围的单引号更改为双引号:

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

      或使用. 运算符进行字符串连接:

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

      PHP's Strings documentation 提到了这两种技术。

      【讨论】:

        猜你喜欢
        • 2020-02-05
        • 2019-01-20
        • 1970-01-01
        • 2013-10-04
        • 2019-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多