duankeng9477 2018-06-08 02:13
浏览 152

如果变量为空,则使用另一个值

  • I am using an multi dim. array from a box packing API to feed values into the United States Postal Service's API to get shipping quotes. I needed my code to work in the case of one package as well as in the case of multiple packages.

I am getting the error "Notice: Undefined variable: length3" I have tested it and the variable is indeed empty(I don't have a 3rd package). So i'm not sure why I can't define it in the case that the original variable is empty. The code that is undefined:

$length3 =$transfer[0][2];

The error actually points to here in the code (line with $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>
";

I have tried:

$length3 = $length3 ?: '0';

and

if (empty($length3)){
$length3='0';
}

and

$length3 =$transfer[0][2] ?? '';

None of them seem to work. I'm puzzled as I thought at least one of those would work.

Please help. Thank you in advance.

  • 写回答

3条回答 默认 最新

  • dongzhi1949 2018-06-08 02:20
    关注

    Maybe you intended:

    $length3 = (empty($length3) ? '0' : $length3);
    
    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接