1

I am working on PHP, today I got an issue with string null. I tried to validate too many things with GOOGLE, but not the success. So I came here to clarify.

I got value of $var1 = "null"

To validate above I tried the following ways.

 $null = is_null($var1);

            if($null){
                $var1 = null;
            }

            if($var1 === null){
                $var1 = null;
            }

            if($var1 === "null"){
                $var1 = null;
            }

But not successful above ways. How do I validate $var1 = "null"?

2
  • 1
    if($var == 'null'){/* do stuff */} 'null' != null, 'null' is a string, which is a value. Commented Jan 27, 2017 at 6:12
  • You can't do like this as is_null($string) returns boolean and so you can not compare that to null or 'null' Commented Jan 27, 2017 at 6:13

4 Answers 4

12

You can validate in many ways :

  1. By using following php variable handling functions

    • is_null(mixed $var)
    • isset(mixed $var)
    • empty(mixed $var)
  2. By using comparison operator ==, === or !=

is_null(mixed $var)

You can use php variable handling function is_null(mixed $var) which returns TRUE if var is null, otherwise FALSE.

<?php
 $foo = NULL;
 $bar = 'NULL';
 var_dump(is_null($foo), is_null($bar));
?>

OUTPUT

bool(true) bool(false)

As you can see $bar = 'NULL' and $bar = NULL both are different thing. Actually, in one it is initializing with a string, not with NULL.


isset(mixed $var)

It returns TRUE if var exists and has value other than NULL, FALSE otherwise.

Note

isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use, the defined() function.

<?php
     $foo = NULL;
     $bar = 'NULL';
     var_dump(isset($foo), isset($bar));
?>

OUTPUT

bool(false) bool(true)

empty(mixed $var)

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

<?php
     $foo = NULL;
     $bar = 'NULL';
     var_dump(empty($foo), empty($bar));
?>

OUTPUT

bool(true) bool(false)

== or === or !=

You can use comparison operator ==, === or != to check whether a variable is null or not.

   <?php
     $foo = NULL;
     $bar = 'NULL';

// Using == operator   
     echo "Using '==' operator\n";
     if($foo == NULL)

    echo "foo is NULL\n";

     else

    echo "foo is not NULL\n";

     if($bar == NULL)

    echo "bar is NULL\n";

     else

    echo "bar is not NULL\n";

// Using === operator 
     echo "\nUsing '===' operator\n";
     if($foo === NULL)

    echo "foo is NULL\n";

     else

    echo "foo is not NULL\n";

     if($bar === NULL)

    echo "bar is NULL\n";

     else

    echo "bar is not NULL\n";

?>

OUTPUT

Using '==' operator
foo is NULL
bar is not NULL

Using '===' operator
foo is NULL
bar is not NULL

The only difference between == and === is that == just checks to see if the left and right values are equal. But, the === operator (note the extra “=”) actually checks to see if the left and right values are equal and also checks to see if they are of the same variable type (like whether they are both booleans, ints, etc.).


Sign up to request clarification or add additional context in comments.

Comments

2

You are mixing variable types, I guess you want to compare a null value, but you set the var as:

$var1 = "null";

This is a string. In this case, your third attempt is working correctly:

if($var1 === "null"){
    $var1 = null;
}

It is using the identical operator (same type, same value) and as the values are identical, it sets the variable to NULL.

If you want the is_null function to work, set the $var1 to null:

$var1 = null;
if(is_null($var1)){
    // ok now we are here
}

Remember that is_null() returns a boolean value so you can inline it into the if statement.

Comments

1

Here is A more detail answer but I would like to addon if you are dealing with &nbsp;.

This would work if dealing with &nbsp;, replace it with str_replace() first and check it with empty()

empty(str_replace("&nbsp;" ,"" , $YOUR_DATA)) ? $YOUR_DATA = '--' : $YOUR_DATA;

Comments

1

In my way

function expDate($getDate) {
    if(is_null($getDate)){
        return null;
    }else{
        $dob = $getDate;
        $result= explode('-',$dob);
        $date= $result[2];
        $month= $result[1];
        $year= $result[0];
        return $date.'-'.$month.'-'.$year;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.