Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

code sustitution

Tags:

php

casting

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?

like image 226
Luis Siquot Avatar asked Sep 28 '26 01:09

Luis Siquot


2 Answers

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.

like image 185
Rijk Avatar answered Sep 29 '26 15:09

Rijk


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

like image 30
duedl0r Avatar answered Sep 29 '26 14:09

duedl0r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!