Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting variables using logcial OR

$db_hased_pw = $result["password"] || false;

I understand the usage of this line of code, but WHEN will it evaluate as false?

will $db_hased_pw equal false only when $result["password"] is undefined?

or is $db_hased_password be set to false if $result["password"] is unset, or false, or zero, or null?

like image 690
AlexMorley-Finch Avatar asked Jan 24 '26 10:01

AlexMorley-Finch


1 Answers

It will evaluate to false when $result["password"] is a "falsy value". The page on empty describes these values.

These would be equivalent:

$db_hased_pw = !!$result["password"];
$db_hased_pw = (bool) $result["password"];

If $result["password"] can indeed be undefined, you should be using:

$db_hased_pw = !empty($result["password"]);

to avoid a notice.

like image 200
Artefacto Avatar answered Jan 26 '26 00:01

Artefacto



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!