【问题标题】:concatenating a number with string not parsed in PHP [closed]将数字与未在 PHP 中解析的字符串连接起来 [关闭]
【发布时间】:2013-08-28 02:33:11
【问题描述】:

我的 PHP 代码中有一个简单的连接,它将几个字符串连接在一起以发出 XML 请求。出于某种原因,当我将 customerId 与字符串链连接时,它不会被解析。但是如果我在单引号内传递一个常数,它就会被解析。这是一个例子。

如果我用一个常数连接我,像这样:

$out='<?xml version="1.0"?>';
$out=$out.'<soapenv:Envelope ';
$out=$out.'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ';
$out=$out.'xmlns:v1="http://www.test.com/v1">';
$out=$out.'<soapenv:Header/>';
$out=$out.'<soapenv:Body><v1:loadData><customerNumber>';
$out=$out.'2985634';
$out=$out.'</customerNumber></v1:loadData>';
$out=$out.'</soapenv:Body>';
$out=$out.'</soapenv:Envelope>';

回应 $out 会给我:

<?xml version="1.0"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.test.com/v1"><soapenv:Header/><soapenv:Body><v1:loadData><customerNumber>2985634</customerNumber><version>1</version></v1:loadData></soapenv:Body></soapenv:Envelope>

但是如果我传递一个变量而不是一个常数(这是我通过 POST 方法从 from 传递的),它不会被解析,如下所示:

$custId = $_POST['customerId'];
$out='<?xml version="1.0"?>';
$out=$out.'<soapenv:Envelope ';
$out=$out.'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ';
$out=$out.'xmlns:v1="http://www.test.com/v1">';
$out=$out.'<soapenv:Header/>';
$out=$out.'<soapenv:Body><v1:loadData><customerNumber>';
$out=$out.$custId;
$out=$out.'</customerNumber></v1:loadData>';
$out=$out.'</soapenv:Body>';
$out=$out.'</soapenv:Envelope>';

echo 的输出将是:

<?xml version="1.0"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.test.com/v1"><soapenv:Header/><soapenv:Body><v1:loadData><customerNumber></customerNumber><version>1</version></v1:loadData></soapenv:Body></soapenv:Envelope>

我尝试了不同的连接和字符串化变量的方法,但都没有奏效:(

【问题讨论】:

  • 顺便说一句,使用.= ;)
  • 我在这里有一个工作示例,还有codepad.org/ZGhdJj4Q
  • 您的 XML 与您的代码不一致。
  • 我也试过 .= 。没用。 XML 字符串中的某些东西搞砸了,无法弄清楚。
  • 您在 $custId=2985634 之后忘记了 ( ; )

标签: php concatenation string-concatenation


【解决方案1】:

您的代码运行没有问题,这是我的浏览器中此代码的结果:

<?php
    $_POST['id']=2985634;
    $out='<?xml version="1.0"?>';
    $out=$out.'<soapenv:Envelope ';
    $out=$out.'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ';
    $out=$out.'xmlns:v1="http://www.test.com/v1">';
    $out=$out.'<soapenv:Header/>';
    $out=$out.'<soapenv:Body><v1:loadData><customerNumber>';
    $out=$out.$_POST['id'];
    $out=$out.'</customerNumber></v1:loadData>';
    $out=$out.'</soapenv:Body>';
    $out=$out.'</soapenv:Envelope>';
    echo $out;
?>

【讨论】:

  • 感谢您的测试!现在,我可以确定问题来自另一个来源。很有帮助!
  • 我在 chrome 中看不到结果,我建议使用 internet explorer 或 firefox,或者只在 chrome 中查看源代码
【解决方案2】:

回显 XML 输出后,使用查看源查看输出结果,而不是仅在浏览器上查看。

【讨论】:

    【解决方案3】:

    $custId 定义为字符串。

    $custId = '2985634';
    

    另外,不要使用$out=$out.'string';,而是尝试使用$out .= 'string';

    【讨论】:

    • $custId 是作为 POST 方法发送的表单输入。我怎样才能把它变成字符串?
    • 您可以使用strval()将int转换为字符串:$custId = strval($_POST['customerId']);
    • -1 因为你可以做 $custId=213123;然后将其连接到一个字符串示例: echo $custId.'test';
    • 你能把你所说的写成一段代码吗?或者编辑我的代码,好吗?
    • @FaceOfJock 我见过 PHP 做一些奇怪的事情,而不是拒绝连接整数。如果某些东西很时髦,使用 strval() 也会引发错误。
    猜你喜欢
    • 2014-08-30
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    相关资源
    最近更新 更多