Php difference between 0 and null

Asked 13 years, 11 months ago

Viewed 163k times

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 ===?

Ben

9,8685 gold badges40 silver badges42 bronze badges

asked Sep 26, 2008 at 2:53

stalepretzelstalepretzel

15.1k21 gold badges75 silver badges90 bronze badges

2

It's language specific, but in PHP :

Null means "nothing". The var has not been initialized.

False means "not true in a boolean context". Used to explicitly show you are dealing with logical issues.

0 is an int. 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 found 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 DebugMode = False [set to off], DebugMode = True [set to on] and DebugMode = Null [not set at all, will lead to hard debugging ;-]].

Robert

1,2461 gold badge17 silver badges37 bronze badges

answered Sep 26, 2008 at 11:50

e-satise-satis

559k108 gold badges291 silver badges327 bronze badges

12

null is null. false is false. Sad but true.

there's not much consistency in PHP [though it is improving on latest releases, there's too much backward compatibility]. Despite the design wishing some consistency [outlined in the selected answer here], it all get confusing when you consider method returns that use false/null in not-so-easy to reason ways.

You will often see null being used when they are already using false for something. e.g. filter_input[]. They return false if the variable fails the filter, and null if the variable does not exists [does not existing means it also failed the filter?]

Methods returning false/null/string/etc interchangeably is a hack when the author care about the type of failure, for example, with filter_input[] you can check for ===false or ===null if you care why the validation failed. But if you don't it might be a pitfall as one might forget to add the check for ===null if they only remembered to write the test case for ===false. And most php unit test/coverage tools will not call your attention for the missing, untested code path!

Lastly, here's some fun with type juggling. not even including arrays or objects.

var_dump[ 0 true [string match]
'*' === true -> false [numberic match]

[int]'*' == true -> false
[string]'*' == true -> true

PHP7 strictness is a step forward, but maybe not enough. //web-techno.net/typing-with-php-7-what-you-shouldnt-do/

answered Nov 4, 2018 at 12:02

Is 0 and NULL the same in PHP?

PHP considers null is equal to zero.

Is 0 true or false in PHP?

0 is the integer value of zero, and false is the boolean value of, well, false.

Why is NULL not equal to zero?

You can think of it as this, Null just means the value is "undefined". But "0" defines the variable to be an integer, 0 is a value! Null is not declared to be anything. Therefore it is false.

Is 0 considered empty PHP?

PHP empty[] Function This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0. 0.0.

Chủ Đề