17.6 C
Jaipur
Thursday, January 20, 2022

is_null function in PHP

Must read

The null value is used in PHP to indicate that no specific value is assigned to the variable. The null value does not define that the value is empty, and the value of the variable will be null if the null value is assigned to it. PHP has many built-in functions for testing data. The is_null() function is one of them that is used to check whether the variable contains the null value or not. The different ways of using the is_null() function in PHP are provided in this tutorial.

The syntax of the is_null() function has given below.

Syntax

This function can take one argument only, and it returns true if the argument value is null or NULL and false for other values.

Different Uses of is_null() Function

The uses of the is_null() function for different purposes have been shown in this tutorial using multiple examples.

Example 1: Use of is_null() Function to Test the Different Types of Values
Create a PHP file with the following script to check the return value of the is_null() function for null, empty string, and NULL values. The check_null() function has been defined in the script to check three types of value. The function is called with the null value the first time, an empty string the second time, and a NULL value the third time:

<?PHP

    //Declare a function to check the null value
    function check_null($var)
    {
        if (!is_null($var))
            echo ‘The value of the variable is not NULL.<br/>’;
        else
            echo ‘The value of the variable is NULL<br/>’;
    }

    //Assign a variable with null value
    $variable = null;
    check_null($variable);

    //Assign a variable with empty string
    $variable = ;
    check_null($variable);

    //Assign a variable with NULL value
    $variable = NULL;
    check_null($variable);

?>

Output
The following output will appear after executing the previous script. According to the output, the is_null() function has returned true for the null and NULL values and false for the empty string:

Example 2: Check the Output of is_null() After Unsetting the Variable
Create a PHP file with the following script to check the return value of the is_null() function after unsetting the variable. The error_reporting() function has been used in the script to disable the notice information from the output. The null value has been assigned into a variable, and the returned value of the is_null() function for that variable has been checked. Next, the unset() function has been used to unset the variable. The returned value of the is_null() function has been rechecked for that variable:

<?php

//Disable the E_NOTICE error
error_reporting(E_ALL & ~E_WARNING );

//Assign a null value
$variable = null;
echo “The output of the is_null() function: “;
//Check the dump value
echo is_null($variable);
echo “<br/>”;

//Unset the variable
unset($variable);
echo “The output of the is_null() function after unset: “;
//Check the dump value after unset
echo is_null($variable);

?>

Output
The following output will appear after executing the above script. According to the output, the is_null() function has returned 1 before and after unsetting the variable. That means the is_null() function returns true for the undefined variable also.

Example 3: Use of is_null() Function Inside var_dump() Function
Create a PHP file with the following script to check the dump values of string, NULL, and number by using the is_null() function inside the var_dump() function. Three types of values have been initialized in three variables. Next, these variables have used in the is_null() function which is used inside the var_dump() function. Three returned values of the is_null() function will be dumped after executing the script:

<?php

//Initialize three variables
$var1 = ‘Linux Hint’;
$var2 = NULL;
$var3 = 634;

//Check the dump values of the variable
echo “<b>The output of the is_null() function of multiple variables:</b><br/>”;
var_dump(is_null($var1), is_null($var2), is_null($var3));

?>

Output
The following output will appear after executing the previous script. According to the output, the is_null() function has returned false for the string value, true for the NULL value, and false for the number:

Example 4: Use of is_null() Function in the Form Output
Create a PHP file with the following script to set the NULL value for a field if the field value is empty. An HTML form has been designed in the script that has three fields and one submit button. The name field is mandatory, while the email and phone fields are optional. If the user does not submit any value in the email field, then the values of the email field will be set to NULL. The $_POST array contains the values submitted by the form. The dump values of the $_POST array have been printed later to check all values of the array:

<html>
<body>
    <form action=“#” method=“post”>
        Enter full name: <input type=“text” name=“name” required /><br/>
        Enter email address:<input type=“text” name=“email” /><br/>
        Enter phone:<input type=“text” name=“phone” /><br/>
        <input type=“submit” name=“sub” value=“Submit” /><br/>
    </form>
</body>
</html>
<?php

//Check the form is submitted or not
if(isset($_POST[‘sub’]))
{
    //Check the email field is empty or not
    if($_POST[’email’] == “” )
    //Set null to email if the field is empty
    $_POST[’email’] = null;

    //Print the dump value of the $_POST array
    var_dump($_POST);
}

Output
The following output will appear after executing the previous script. Keep the email field empty before submitting the form to assign a null value to the field:

The following output will appear after submitting the form by keeping the email and phone fields empty. The dump output shows that NULL value has been assigned in the email field for submitting the form by keeping this field empty.

Conclusion

It is important to validate the data before inserting any data into the database table of any web application. The is_null() function is one of the useful functions for validating data. The is_null() function returns true for which types of values and different uses of this function have been explained in this tutorial to help the PHP users discover the purposes of using this function in PHP. We hope you found this guide helpful. Follow Linux Hint for more tips and information.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article