Sometimes we have confusion using the right variable handler in PHP. When we want to test the availability of a variable, we use isset. After that we wanted to further examine the value of a variable is null or no content. There are several ways to specify these conditions in PHP. Here are some cheatsheet that you can use.
In this table, column represent condition or variable handler, and row represent value and it's result.
Handler | isset | is_null | ==null | ===null | empty |
---|---|---|---|---|---|
null | false | true | true | true | true |
boolean true | true | false | false | false | false |
boolean false | true | false | true | false | true |
1 | true | false | false | false | false |
0 | true | false | true | false | true |
unset | false | true | true | true | true |
"" | true | false | true | false | true |
\0 | true | false | false | false | false |
Base on cheatsheet above, we got several conclosion that:
- empty and ==null are equivalent
- isset is inverse of is_null and ===null result
- is_null and ===null are equivalent
That's all. Hope this will help you accelerate in your coding. Thank you for your visit. Comment below to take in disqussion.