I am told that good developers can spot/utilize the difference between Null and False and 0 and all the other good ‘nothing’ entities.
What is the difference, specifically in PHP? Does it have something to do with ===?
I am told that good developers can spot/utilize the difference between Null and False
Share
It’s language specific, but in PHP :
Nullmeans "nothing". The var has not been initialized.Falsemeans "not true in a boolean context". Used to explicitly show you are dealing with logical issues.0is anint. Nothing to do with the rest above, used for mathematics.Now, what is tricky, it’s that in dynamic languages like PHP, all of them have a value in a boolean context, which (in PHP) is
False.If you test it with
==, it’s testing the boolean value, so you will get equality. If you test it with===, it will test the type, and you will get inequality.So why are they useful ?
Well, look at the
strrpos()function. It returns False if it did not find anything, but 0 if it has found something at the beginning of the string!And of course, if you deal with states, you want to make a difference between the following:
DebugMode = False(set to off)DebugMode = True(set to on)DebugMode = Null(not set at all; will lead to hard debugging ;-))