-2

After upgrading PHP version from 7.3 to 7.4 I'm getting one deprecated issue.

[27-Nov-2020 01:14:32 UTC] PHP Deprecated:  Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in /home/lankalk/public_html/chat/sender.php on line 364

In line 364 and 365 of sender.php has following code:

$img_ext = ($image_sizes[2] == 1)? "gif":
            ($image_sizes[2] == 2) ? "jpg":"png";

Please suggest me code upgrade

1 Answers1

1

It looks like you wanted to have something like this:

if ($image_sizes[2] == 1) {
    $img_ext = "gif";
} elseif ($image_sizes[2] == 2) {
    $img_ext = "jpg";
} else {
    $img_ext = "png";
}

However, this is not what your ternary does at the moment. It looks like it was a bug.

If you would like to keep a nested ternary (which is not recommended as the if statement is easier to understand) then you need to use parentheses to specify which condition is first.

$img_ext = $image_sizes[2] == 1
    ? "gif"
    : (
        $image_sizes[2] == 2 // elseif
        ? "jpg"
        : "png" // else
    );
Dharman
  • 30,962
  • 25
  • 85
  • 135