Saic Siquot
Saic Siquot

Reputation: 6513

code sustitution

can I replace

if(!$mail->Send()) {
    return false;
} else {
    return true;
}

with this?

return (bool)$mail->Send();

regardless of what $mail->Send() is returning (it is a three party object)
is there any type where (bool) acts diferent of the exclamation mark?

Upvotes: 3

Views: 74

Answers (5)

Rijk
Rijk

Reputation: 11301

Yes, typecasting to the following types is allowed:

The casts allowed are:

  • (int), (integer) - cast to integer
  • (bool), (boolean) - cast to boolean
  • (float), (double), (real) - cast to float
  • (string) - cast to string
  • (array) - cast to array
  • (object) - cast to object
  • (unset) - cast to NULL (PHP 5)

Otherwise, it's also possible to use

return !!$mail->Send();

however, that's not very obvious for someone else looking through your code in the future.

Upvotes: 3

duedl0r
duedl0r

Reputation: 9424

Yes you can, and if it's not working you could do this: return !!$mail->Send();.

Upvotes: 1

Abbas
Abbas

Reputation: 14432

As long as the return-type of the Send() function is of the boolean type you can use this:

return $mail->Send();

Upvotes: 0

jondavidjohn
jondavidjohn

Reputation: 62392

Yes, your replacement works fine...

return (boolean)$mail->Send();

That if statement is written in a very strange way, the ! mark is a logical NOT and simply reverses the bool, so it says...

if $mail->Send() is NOT true, return false otherwise return true.

Upvotes: 1

Beez
Beez

Reputation: 2101

You could simplify it like:

return $mail->Send() ? true : false;

Upvotes: 1

Related Questions