Php empty string vs null

Null is just another datatype in PHP, which has only one value (null). Since PHP is a loosly typed language, it can be confusing how it handles different values.

"", 0, "0", False, array(), Null are all considered False in PHP.

Null, however, is a different kind of animal. The main incompatibility with using Null is that you cannot tell if it isset().

$x = false;
isset($x)  ->  true
echo $x    ->  ""

$y = null;
isset($y)  ->  false
echo $y    ->  ""

//$z is not set
isset($z)  ->  false
echo $z    ->  E_NOTICE

So null is odd in the sense that it doesn't follow normal variable rules in PHP (at least some). In most cases, it is fine.

When it comes to database columns, PHP's NULL has no place there. You see, SQL is a string based language. SQL's NULL must be represented by NULL with no quotes.

So if you want an EMPTY field, set it to ""

INSERT INTO foo SET bar = ""

But if you want a NULL field, set it to NULL

INSERT INTO foo SET bar = NULL

BIG DIFFERENCE.

But if you try to insert the PHP NULL directly, it will add zero characters to the query, (which leaves you with a blank or syntax error, depending on if you quoted it).

You will use variables in almost every program that you write using PHP. Most of the time these variables have a value, and we usually create them with an initial value. However, there is always a possibility that some of the variables you are using are not initialized. This can result in a warning from PHP about using an undefined variable.

There can be many reasons for undefined variables. The most common ones are that you either actually did not define the variable or you made a spelling mistake when reusing it somewhere else. This is just a programming bug. However, another possibility that can result in an undefined variable is that it has been defined conditionally.

You also might find that a variable has the value NULL. This also can happen for a number of reasons. For example, the variable might just have not been initialized with a value. Or the null value might be returned from a function to signal some sort of error.

In any case, using a variable before it has been defined or when it has a null value can have unintended consequences. In this tutorial, I'll show you how to check if an element has been defined and see if it is empty or null.

You can use isset(), empty(), or is_null() to check if either one or all of those conditions are true or false.

Definitions

Let's get started with some definitions.

  1. isset(): You can use isset() to determine if a variable is declared and is different than null.
  2. empty(): It is used to determine if the variable exists and the variable's value does not evaluate to false.
  3. is_null(): This function is used to check if a variable is null.

PHP isset() vs. empty()

As we saw from the definitions, isset() will return true if we have defined the variable before and set its value to something other than NULL. This can include 0, an empty string, or false. On the other hand, empty() will return true whenever the variable value is set to something that evaluates to false—we call these "falsey" values. Examples of falsey values include 0, the empty string "" and the string "0", an empty array, NULL, or of course the boolean false.

One similarity between isset() and empty() is that they are both language constructs and therefore cannot be called using variable functions.

The following code snippet should explain the difference between these two.

Note that empty() can be written using the isset() function:

Of course, it's usually just easier to use the built-in empty() function.

PHP isset() vs. is_null()

The is_null() function returns true if the value of a variable has been explicitly set to NULL. Otherwise, it simply returns false. On the other hand, isset() will return true as long as a variable is defined and its value is not NULL.

Here is a basic example to show the difference between them.

PHP empty() vs. is_null()

The empty() function returns true if the value of a variable evaluates to false. This could mean the empty string, NULL, the integer 0, or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL.

Here is a basic example to show the difference between them.

 'Monty', 'last_name' => '', 'age' => '83', 'fav_movie' => NULL];

if(empty($person['last_name'])) {
    if(is_null($person['last_name'])) {
        echo 'The last name is set to NULL.';
    } else {
        echo 'The last name is probably an empty string.';
    }
}
// Output: The last name is probably an empty string.

if(is_null($person['fav_movie'])) {
    echo $person['first_name'].' did not specify a favorite movie.';
}
// Output: Monty did not specify a favorite movie.

Important Points to Remember

There are two tips that you can use to write more concise code and avoid errors in future.

1. Unlike empty() and is_null(), you can pass multiple values to isset() at once to simultaneously check if any of them is undefined or set to NULL. In that case, isset() will only return true if none of the passed values are NULL.

2. Don't use == to check if a value is NULL. This will give a false positive for falsey values like an empty string that evaluate to false.

Final Thoughts

This tutorial quickly explained the difference between isset(), empty(), and is_null(). Hopefully, you will now be able to determine which of them is right for you to use in your code.

Did you find this post useful?

Php empty string vs null

Freelancer, Instructor

I am a full-stack developer who also loves to write tutorials. After trying out a bunch of things till second year of college, I decided to work on my web development skills. Starting with just HTML and CSS, I kept moving forward and gained experience in PHP, JavaScript and Python. I usually spend my free time either working on some side projects or traveling around.

Is empty string same as NULL PHP?

The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL . Here is a basic example to show the difference between them.

Is it better to use NULL or empty string?

So, NULL is better. An empty string is useful when the data comes from multiple resources. NULL is used when some fields are optional, and the data is unknown.

Is empty string and NULL are same?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.

Is empty string true in PHP?

The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.