我收到错误"注意:未定义的变量:length3" 我测试了它,变量确实是空的(我没有第3个包)。所以我不确定为什么在原始变量为空的情况下我无法定义它。 未定义的代码:
$length3 =$transfer[0][2];
错误实际上指向代码($ length3 var):
$data = "API=RateV4&XML=<RateV4Request USERID=\"$userName\">
<Revision>2</Revision><Package ID=\"1ST\">
<Service>ALL</Service>
<ZipOrigination>$orig_zip</ZipOrigination>
<ZipDestination>$dest_zip</ZipDestination>
<Pounds>0</Pounds>
<Ounces>$ounces</Ounces>
<Container>VARIABLE</Container>
<Size>REGULAR</Size>
<Width>$width</Width>
<Length>$length</Length>
<Height>$height</Height>
<SpecialServices>
<SpecialService>100</SpecialService>
</SpecialServices>
<Machinable>TRUE</Machinable></Package>
<Package ID=\"2ND\">
<Service>ALL</Service>
<ZipOrigination>$orig_zip</ZipOrigination>
<ZipDestination>$dest_zip</ZipDestination>
<Pounds>0</Pounds>
<Ounces>$ounces2</Ounces>
<Container>VARIABLE</Container>
<Size>REGULAR</Size>
<Width>$width2</Width>
<Length>$length2</Length>
<Height>$height2</Height>
<Girth></Girth>
<Value></Value>
<SpecialServices>
<SpecialService></SpecialService>
</SpecialServices>
<Machinable>TRUE</Machinable></Package>
</Package>
<Package ID=\"3RD\">
<Service>ALL</Service>
<ZipOrigination>$orig_zip</ZipOrigination>
<ZipDestination>$dest_zip</ZipDestination>
<Pounds>0</Pounds>
<Ounces>$ounces3</Ounces>
<Container>VARIABLE</Container>
<Size>REGULAR</Size>
<Width>$width3</Width>
<Length>$length3</Length>
<Height>$height3</Height>
<Girth></Girth>
<Value></Value>
<SpecialServices>
<SpecialService></SpecialService>
</SpecialServices>
</Package></RateV4Request>
";
我试过了:
$length3 = $length3 ?: '0';
和
if (empty($length3)){
$length3='0';
}
和
$length3 =$transfer[0][2] ?? '';
它们似乎都不起作用。我很困惑,因为我认为其中至少有一个会起作用。
请帮忙。提前谢谢。
答案 0 :(得分:0)
也许你打算:
$length3 = (empty($length3) ? '0' : $length3);
答案 1 :(得分:0)
If(isset($var) && $var){
echo "1";
}else{
echo "0";
}
答案 2 :(得分:0)
试试这个
$length3 = empty($transfer[0][2]) ? '0' : $transfer[0][2];