sunjie
sunjie

Reputation: 2053

Question about PHP function return

I want to check if any of the three functions inside the IF did not execute successfully. If any of them did not run, i want to get a return value false.

if($ext == "gif" or $ext == "png"){
    imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
    imagealphablending($new, false);
    imagesavealpha($new, true);
    }

For example I want to know if all the three functions imagecolortransparent, imagealphablending, imagesavealpha did execute successfully, if not, return false. Do i need to check each function like following or is there a better way?

if($ext == "gif" or $ext == "png"){
    if (!@imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)))
        return false;
    if (!@imagealphablending($new, false))
        return false;
    if (!@imagesavealpha($new, true))
        return false;
}

Thanks.

Upvotes: 1

Views: 197

Answers (6)

hakre
hakre

Reputation: 197767

I think what you do is quite okay, you can concatenate the expressions however. It's doing the same, and in case a function returns false, the expression will not be followed for the rest (runtime optimization):

if($ext == "gif" or $ext == "png"){
    if (
        @imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127))
        and @imagealphablending($new, false)
        and @imagesavealpha($new, true)
    ) return true;
    return false;
}

Or by using a variable and sparing the second if thus making it more expressive:

if($ext == "gif" or $ext == "png")
    return 
      @imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127))
      && @imagealphablending($new, false)
      && @imagesavealpha($new, true)
    ;

Upvotes: 0

Mithun Satheesh
Mithun Satheesh

Reputation: 27855

  if($ext == "gif" or $ext == "png"){
if ((!@imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)))
    || (!@imagealphablending($new, false))
    || (!@imagesavealpha($new, true)) )
   {
        return false;
   }

}    

this will also do the same function. But in both the way you wont know which function failed.

Upvotes: 1

Icode4food
Icode4food

Reputation: 8694

Below is how I would do it.

if($ext == "gif" or $ext == "png"){
  $return = TRUE;
  $return = $return && imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
  $return = $return && imagealphablending($new, false);
  $return = $return && imagesavealpha($new, true);
  return $return;
}

Basically I am And-ing all the responses to verify that they are all true. If any one of them returns FALSE, the function will return FALSE.

There are more beautiful ways to do it, but I find this way to be reasonably clear exactly what is happening.

Upvotes: 0

KingCrunch
KingCrunch

Reputation: 131881

if (!@imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127))
  || !@imagealphablending($new, false)
  || !@imagesavealpha($new, true)) return false;

or just

return (@imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127))
     && @imagealphablending($new, false)
     && @imagesavealpha($new, true));

Don't know, if the @ is required here. You should try to avoid it.

Upvotes: 0

tdammers
tdammers

Reputation: 20721

You can set up a custom error handler that translates all PHP errors into ErrorExceptions and register it globally. Once you've done that, an error in any of the three functions will raise an exception, which you can handle with a try/catch block.

Upvotes: 0

gnur
gnur

Reputation: 4733

You can join them in 1 single if, but your method is probably the better way because it won't execute the following statements if one of the function calls fails.

Upvotes: 0

Related Questions