BlackHatSamurai
BlackHatSamurai

Reputation: 23483

empty() not working in PHP

I am working with PHP 5.4 and I am submitting a form via POST request, and I want to check to see if one of the values is null, or populated. However the condition does not appear to resolving to true and I can't figure out what I'm doing wrong.

Here is the code:

        $route = $_POST['route']; 
        echo ("route: " . $route);//this is displaying 'route:null'


        if(empty($route))
        {
            echo ("route2: " . $route);                 
        }

I have also tried to use isset is_null and $route === null $route == null

None seem to work .

I have also tried inserting true into the if statement to make sure that true is resolving correctly, but it does.

Upvotes: 0

Views: 6732

Answers (6)

Murat Kezli
Murat Kezli

Reputation: 307

Try This ;)

function convert_null($data)
        {
        $data = str_replace(null,"",$data);     
        $data = str_replace("null","",$data);
        $data = trim($data);
        return $data; 
        };
        
        
    convert_null($route);
    
    if(empty($route))
                {
                    echo ("route2: " . $route);                 
                };

Upvotes: 0

dkasipovic
dkasipovic

Reputation: 6120

If the echo is displaying null then it is not null. It is a string "null" which are two entirely different things.

This, in fact is a common problem when using javascript to post to php. Javascript's null gets converted to string when posting to php, thus passing all checks (empty(), isset(), ...).

The easiest way to see these issues is to do a var_dump($_POST), which will give you exactly what you have received in your post.

In your case you are receiving string "null", which in no way can pass the empty() check. You either need to check if $route=="null" or fix your javascript so it does not send null values.

Fixing javascript is the proper way to go.

Upvotes: 1

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

Because the $_POST is returning null as a string I had to do this:

        $route = $_POST['route']; 
        echo ("route: " . $route);


        if($route == 'null')//surround null in quotes
        {
            echo ("route2: " . $route);

        }

Upvotes: 0

Sammitch
Sammitch

Reputation: 32232

if( isset($_POST['route']) && !empty($_POST['route']) ) {
  $route = $_POST['route'];
  echo "Route: $route";
} else {
  echo "Route: None specified";
}

Upvotes: 0

Nikko R.
Nikko R.

Reputation: 178

Empty - Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

How about try using (or adding):

if(is_null($route))

or

if(!isset($route))

Upvotes: 0

Shawn
Shawn

Reputation: 3369

The POST variable does not contain anything. Either it does not exist on the form or you have it misspelled.

According to the docs null is empty.

https://www.php.net/empty

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

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)

Upvotes: 1

Related Questions