Null Variables in PHP

A Null variable is a variable that doesn’t have a value. While writing code, there can be situations/scenarios where we have to deal with Null variables. In fact it was the same fact that led me to write about this topic as well. In this post I am going to show you such scenarios where a variable is considered Null in a PHP script.

When creating a variable in PHP, you don’t always need to assign a value to it. You can just define the variable without a value by skipping its assignment part as shown in the below example:

<?php
$username; // this is valid

As you can see in the above example, we have defined a variable called $username but we have intentionally skipped assigning a value to it. When you have created a variable in this way, then that variable is considered a NULL variable.

Another situation where a variable can become NULL is that when you use the PHP function unset() to destroy a variable, that will also designate that variable as NULL.

For example, when you do something similar to what has shown below:

<?php
unset($variable);

it will destroy that variable. That means, after unsetting a variable you won’t be able to access it’s value later. unset() makes it null. Check out the below examples to see more of this in action:

<?php
# step 1: set a variable
$person = 'Bill Gates';
 
# step 2: show $person
echo "Person's name is $person";
 
# add some space below
echo "<br><br>";
 
# step 3: unset $person
unset($person);
 
# step 4: show the $person again
echo "Person's name is $person";

When you run the above code, you should see an output similar to the following:

Person's name is Bill Gates
Person's name is

What Just Happened?

In the first step of the above code, we have created a variable called $person and assigned the value ‘Bill Gates’. Second step uses $person variable to construct a message that displayed as ‘Person’s name is Bill Gates’. So far so good. In the third step, we have used PHP’s unset() function to destroy the variable. Finally, we again constructed the message as we did in the step 2. But this time you will see ‘Bill Gates’ is missing from the output. That’s because unset() has made $person a null variable. So it doesn’t have a value.

Finally, another scenario where a variable would be considered NULL is when you explicitly assign ‘NULL’ as the value of some variable.

See Below Example:

<?php
# step 1: set a variable
$person = 'Bill Gates';
 
# step 2: show $person
echo "Person's name is $person";
 
# add some space below
echo "<br><br>";
 
# step 3: set $person to NULL
$person = NULL;
 
# step 4: show the $person again
echo "Person's name is $person";

As you can see, we have taken the same code sample that we’ve used in the second example. Only the 3rd step has changed. Instead of unset() this time we have re-assigned $person’s value to NULL. When you run this snippet, the result will be exactly similar to what you saw in the previous example where you’ve used the unset() function. The $person variable still evaluates to null.

That’s it for now guys !!! 👋👋👋

Was this helpful?
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0