I find it's really hard to understand the Null datatype in PHP. Why does it even need to exist? If it's only represent an empty value why PHP doesn't just offer a way to remove it from the code since the variable contains it doesn't have any useful thing to do in our application?
Asked
Active
Viewed 43 times
-4
-
Because some people might act consider a null value to be of value, in function returns for example.... just because you see no value in it, doesn't mean that other people don't find it useful in their applications – Mark Baker Sep 30 '16 at 09:02
-
5it doesn't represent an empty value - it represents that you have a variable, but you *don't* have a value. for example: getSomeObject() can give you an object - or it can give you null to tell you that there was nothing to return. – Franz Gleichmann Sep 30 '16 at 09:04
-
As an example, MS Excel has a null datatype, so if reading a cell containing that from an Excel spreadsheet, what shoudl I return if not a null? An empty string isn't the same thing, that's a different valid value; a boolean true/false is also different and equally valid; so I return a PHP Null instead – Mark Baker Sep 30 '16 at 09:05
-
Possible duplicate of [Null vs. False vs. 0 in PHP](http://stackoverflow.com/questions/137487/null-vs-false-vs-0-in-php) – Frederic Klein Sep 30 '16 at 09:06
1 Answers
1
Consider this:
if (some_condition) $var1 = "Bob";
Now if that condition is false, then $var1
would be null because you did not define it. So, you could have the decision to check if it is null to proceed with what you want to do with it.
if (!is_null($var1)) {
// do something
}
Hope that helps.

Rax Weber
- 3,730
- 19
- 30