The difference between 0, false and null

Published December 7, 2011

There’s been a few things over the years that I’ve never really understood, and it took me quite a while to “get” them. Obviously I tried, but nobody’s descriptions ever made sense. Hopefully, this brief look at this topic might help at least one other person.

I’m going to write about 0, false and null – why they’re different, and why that’s important. I’m also going to write about the difference between a string and an integer, and ways of using all this in if and for.

Let’s start with 0.

0 is a number, as I’m sure you know. In binary it takes the part of “off” or “false”, and 1 is “on” or “true”. This is why 0 and false are often mixed.

There’s two types of 0 in PHP. The first is the integer (meaning, a whole number) 0, and the second is a string.

You’d form these in PHP like so

$var1 = 0;
$var2 = '0';

$var1 is an integer, a whole number. You can use it in maths. $var2 is a string, a word or sentence. It’s just text as far as PHP is concerned, you might as well write the word “zero”.

If you’ve been using PHP for a while, you’ve probably got away without worrying about this and are probably wondering what I’m going about.

if ($var1 == $var2)
{
    echo 'Oh, it worked!';
}

What’s going on here? It’s because of how the operator works.

The key difference is between equal and identical. The if statement I wrote above works because I’m comparing the integer 0 with a string 0. Both values are 0, of sorts.

But what happens if I try using three equals signs.

if ($var1 === $var2)
{
    echo 'Ain’t going to happen!';
}

Now I’m comparing types too, and a string isn’t an integer.

In the computer world, 0 is also used as false, as I discussed earlier. This works for operators too.

$var1 = 0;
$var2 = false;

if ($var1 == $var2)
{
    echo 'Yep, totally worked.';
}

But, again, if I used an identical operator, it would fail.

So why on earth is this important?

Let’s say you’re working with a database. You ask the database to count rows, and then let the user know how many rows there are.

get_number_of_rows is a fictional function I made up that returns either an integer of the number of rows, or it returns false if the database is broken.

$rows = get_number_of_rows();

if ($rows == 0)
{
    echo 'There’s no rows :(';
}

The problem is here, that if the database failed to load, it would return false. Using an equal operator (two equals signs), that matches 0 so the user would see “There’s no rows :(”.

So we use the identical operator to help tell the difference.

$rows = get_number_of_rows();

if ($rows === false)
{
    echo 'Oh dear! Broken!';
}
elseif ($rows === 0)
{
    echo 'No rows :(';
}
else
{
    echo 'There are '.$rows.' rows!';
}

This is still pretty bad though, as just because the row number isn’t false or 0, it could still be a string of some sort. So we’d use a greater than operator to help find out what’s really going on.

if ($rows === false)
{
    echo 'Oh dear! Broken!';
}
elseif ($rows === 0)
{
    echo 'No rows :(';
}
elseif ($rows > 0)
{
    echo 'There are '.$rows.' rows!';
}
else
{
    echo 'Something’s gone really bad';
}

Here comes null

So, this is where it might start to mess with your mind.

0 is still a number, it has a value (even though the value is nothing). false has a value, it means “off”, it’s the opposite of true.

To define a variable as being nothing, you use null.

Null is shared across many languages, not just PHP. A MySQL database will recognise null.

There is a forth part to this love triangle. So it’s sort of a square really. There is the empty string.

$var = '';

If you echo that in PHP, you’ll get nothing. But, it’s still a string. It’s an empty string. That’s different to false, 0 and null.

And that’s why we have null, to tell the difference between an empty string and “nothing”.

null is equal to false, 0 and an empty string.

$var1 = 0;
$var2 = '0';
$var3 = '';
$var4 = false;
$var5 = null;

If you compare any of those using the equal operator == they will all match each other.

But if you use the identical operator ===, they will fail.

To test for null, PHP has a function is_null, which returns a boolean – that means that it’ll return true or false.

Null is most useful when using databases, as rather than storing an empty string for an optional field, you can store a null value. This can be used for error prevention and security amongst other things. It’s also much quicker for a database to select rows using null values, rather than empty strings.

Some databases don’t even let you have empty strings!

Everything is true

If you use false or null as a string, it’ll always return as being true or as a string.

$var1 = "null";

That’s a string for the word “null”, not the value null. That’ll equal true.

isset

So I said that all those variables above are equally comparable. There is one function that ignores this, which is isset. This’ll tell you if a variable is set or not. It returns false if the variable is null. This is useful when displaying data, so that you don’t try and echo something that doesn’t exist and get a notice error.

empty

empty is the opposite to isset, and includes null. Using empty($var) will return true if the variable is any of the following:

Notes

It should be noted that I’ve used null and true in lowercase letters. There is a lot of debate to whether you use uppercase TRUE or lowercase true, but actually it doesn’t matter. The PHP manual always refers to them in uppercase, but most coding style guides prefer lowercase as it’s much easier to read. The most important thing, as with many similar things, is to retain continuity across a project.

Hopefully that explains a bit more about 0, false, null and empty strings.