将数字与PHP中未解析的字符串连接
我的PHP代码中有一个简单的连接,它将几个字符串连接在一起以发出XML请求。出于某种原因,当我将customerId与字符串链连接起来时,它不会被解析。但如果我在单引号内传递一个常量,它就会被解析。这里有一个例子 如果我连接一个常量,如下所示:将数字与PHP中未解析的字符串连接,php,concatenation,string-concatenation,Php,Concatenation,String Concatenation,我的PHP代码中有一个简单的连接,它将几个字符串连接在一起以发出XML请求。出于某种原因,当我将customerId与字符串链连接起来时,它不会被解析。但如果我在单引号内传递一个常量,它就会被解析。这里有一个例子 如果我连接一个常量,如下所示: $out='<?xml version="1.0"?>'; $out=$out.'<soapenv:Envelope '; $out=$out.'xmlns:soapenv="http://schemas.xmlsoap.org/soa
$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='';
$out=$out';
$out=$out';
$out=$out';
$out=$out'2985634';
$out=$out';
$out=$out';
$out=$out';
回显$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>
29856341
但是如果我传递一个变量而不是一个常量(这是我从一个from by POST方法传递的),它将不会被解析,如下所示:
$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>';
$custId=$\u POST['customerId'];
$out='';
$out=$out';
$out=$out';
$out=$out';
$out=$out.$custId;
$out=$out';
$out=$out';
$out=$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></customerNumber><version>1</version></v1:loadData></soapenv:Body></soapenv:Envelope>
1
我尝试了不同的连接和字符串化变量的方法,但都没有成功:(将
$custId
定义为字符串
$custId = '2985634';
另外,不要使用
$out=$out.string';
,尝试使用$out.='string';
您的代码工作正常,下面是我的浏览器中此代码的结果:
<?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;
?>
在回显XML输出后,使用view source检查输出结果,而不是仅在浏览器上查看。使用
=
,顺便说一句;)我这里有一个工作示例,而且您的XML与代码不一致。我也尝试过。没用。XML字符串中的某些内容弄乱了事情,无法理解。您忘记了$custId=2985634之后的(;)$custId是作为POST方法发送的表单输入。如何将其转换为字符串?可以使用strval()
将int转换为字符串:$custId=strval($\u POST['customerId'])代码>-1,因为您可以执行$custId=213123;然后将其连接到一个字符串示例:echo$custId'test';你能把你说的话写成一段代码吗?或者请编辑我的代码?@FaceOfJock我见过PHP做过比拒绝连接int更奇怪的事情。使用strval()
也会在出现问题时抛出错误。感谢您的测试!现在,我可以肯定这个问题来自另一个来源。非常有帮助!我无法在chrome中看到结果,我建议使用internet explorer或firefox,或者只在chrome中查看源代码