Different variables are given values when they are declared. PHP uses these values to determine the data type of the variables.
The PHP programming language can identify a number of data types.
null
data typeWhen you want to create a new variable, your program code tells your computer to leave a memory space that attaches the address of the memory space to the name of the variable. This causes the variable name to point to an address in memory where the stored data can be found.
If you declare a variable and don’t give it a value like the one below, the variable is automatically given a null
value.
$myTravelLog;
null
describes the value of any variable that doesn’t have value. Essentially, null
means no value.
Note: Being
null
does not meanempty
. You will see this difference in the snippet below.
null
To check if a defined variable is null
, we can use two different methods.
isset()
methodThe isset()
method is mainly used to check for null
variables, especially when collecting form data. This helps us avoid keeping non-nullable columns without data.
Let’s take a look at the code snippet to see how this is done.
<?php$x = 0;$plays = "";if(isset($x) && isset($plays)){echo "These are empty not null <br>";//zero is empty in the context of integers}if(empty($x) && empty($plays)){echo "These are empty <br>";// will show us that zero as integer is seen as empty}$jan;if(isset($jan)){echo "This is null <br>";/* This will return false so our echo won't displaybecause variable $jan was not given a value */}$tangle = null;if(isset($tangle)){echo "This is declared as null <br>";}/* This also returns false, because with a null valuevariable $tangle has no value*/?>
In this snippet we can see how the isset()
function returns false
in situations where the variable is null
.
It returns true
when the variable is something else, even if it is empty.
When validating your form input, try to check for empty as well. Don’t just end validation with
isset()
.
is_null()
methodThe is_null()
method is an inbuilt PHP function which that verifies whether or not a variable is null
.
This method is supported by PHP 4 and later versions.
Unlike the isset()
method, the is_null
method returns the following when we use it to check the variable:
true
if the variable is null.false
if the variable is not null.Let’s look at some code.
<?php$a_null_value;$another_null = null;$noneNull = "I have a value.";$thisIsEmpty = '';if(is_null($a_null_value)){//will echo "just null" if it returns//true else nothingecho "just null <br>";}if(is_null($another_null)){//will echo "just another null" if it returns//true else nothingecho "just another null <br>";}if(is_null($noneNull)){//will echo the indicated string if it returns//true else nothingecho "No display cuz this is a string ".$noneNull."<br>";}if(!is_null($thisIsEmpty)){//This will return true because the variable is an//empty string variable and will display the echoecho "will display because ".$thisIsEmpty." is empty";}?>
The code above is an example of how to use the is_null()
function to check if a variable is null
The different if
functions used in the snippet above check the different variables and return the necessary value according to the value of the variables.