- 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.