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

  • 1
    `$length3 = $transfer[0][2] ?? '';` should definitely work if you're using PHP 7+ – fubar Jun 08 '18 at 02:19
  • The line `$length3 =$transfer[0][2];` would not cause an undefined variable error (notice) for the variable `$length3`. Please double-check where the error occurs. The error message should indicate the exact file and line number. – faintsignal Jun 08 '18 at 02:21
  • i'm using PHP/7.2.3 which is why I also thought it was funny that it wasn't working. – user9555327 Jun 08 '18 at 02:25
  • * Question updated to show where variable is undefined in the code. – user9555327 Jun 08 '18 at 02:30
  • I don't think the problem is with *how* you declare `$length3`, but with *where* you declare it. Either it is never declared, or never declared in the scope where the error occurs. Follow the control flow backwards from the point of the error, you will find the real problem. – faintsignal Jun 08 '18 at 02:49
  • My first package (LxWxH & weight) works fine. so I'm not sure how this would be a scope issue. They go from the general scope to a function. – user9555327 Jun 08 '18 at 02:51
  • 1
    There is no data for the 3 package (i.e. $length3) which is what I think is causing the issue. – user9555327 Jun 08 '18 at 02:53
  • Possible duplicate of [PHP ternary operator vs null coalescing operator](https://stackoverflow.com/questions/34571330/php-ternary-operator-vs-null-coalescing-operator) – Gufran Hasan Jun 08 '18 at 04:05

3 Answers3

0

Maybe you intended:

$length3 = (empty($length3) ? '0' : $length3);
Paul T.
  • 4,703
  • 11
  • 25
  • 29
  • I don't understand how this code works could you provide more explanation? For example I thought that the empty() function was only used with an if statement. – user9555327 Jun 08 '18 at 02:46
  • Well, the `ternary` operator IS "like" an if/else. Search for `Ternary Operator` [here](http://php.net/manual/en/language.operators.comparison.php) for an example of the difference. – Paul T. Jun 08 '18 at 02:50
  • Yeah I tried it after my original $length3 = $transfer[0][2]; and it still says my variable is undefined. – user9555327 Jun 08 '18 at 03:03
  • Ok, then try Khrisna's similar ternary answer (though with the direct array index that you have) ... see if that helps. If so, I'll delete this answer and you can accept that one as the answer if it helps. – Paul T. Jun 08 '18 at 03:05
0
If(isset($var) && $var){
echo "1";
}else{
echo "0";
}
Brijesh Dubey
  • 118
  • 2
  • 12
0

Try this instead

$length3 = empty($transfer[0][2]) ? '0' : $transfer[0][2];