Bojjaiah
Bojjaiah

Reputation: 261

How to validate null or empty in php

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"?

Upvotes: 1

Views: 26709

Answers (4)

Hasan Batuhan Kurt
Hasan Batuhan Kurt

Reputation: 281

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;
    }
}

Upvotes: 1

Kopi Bryant
Kopi Bryant

Reputation: 1366

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

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

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

Upvotes: 1

Prashant Pokhriyal
Prashant Pokhriyal

Reputation: 3827

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.).


Upvotes: 12

user3380963
user3380963

Reputation:

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.

Upvotes: 2

Related Questions