简体   繁体   中英

Best way to check for null in PHP

I want to know the best / efficient way to check for null in PHP.

!$var
$var === null
is_null($var)

Don't even know if there are more ways to check it, but I want to use the more efficient one.

I will prefer option two. But I will normally use it this way

null  === $var (yoda condition)

Yoda condition will prevent a possible mistake that you assign a value unintentionally instead of writing a conditional statement.

Frameworks like Wordpress and Symfony use yoda condition as part of their code standard.

Using is_null() function is slower since it is a function. However; the difference is really small.

reference https://en.m.wikipedia.org/wiki/Yoda_conditions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM