check if variable is null - incorrect validation

Go To StackoverFlow.com

0

This problem is a bit strange. Why is showed "Is not null", if the value sent is null? Any reason for that?

Parametersapplication/x-www-form-urlencoded
lists_owned null
Source
lists_owned=null

<?php
$lists_owned = $_POST['lists_owned'];

var_dump($lists_owned); // string(4) "null"

if(!is_null($_POST['lists_owned'])) {
    echo "Is not null"; I see this echo
}
?>  

thanks

2012-04-04 02:26
by user1311784
Ignacio is right, also, check if $_POST['lists_owned'] is set before accessing it : $lists_owned = isset($_POST['lists_owned']) ? $_POST['lists_owned'] : null; and only use the $lists_owned variable after that, it's useless to set $lists_owned and to never use it - mamadrood 2012-04-04 02:31


4

"null" is not null. If you want to check for "null" then you should be using equality.

if($_POST['lists_owned'] != 'null') {
2012-04-04 02:27
by Ignacio Vazquez-Abrams


1

This is because your post value is a string called 'null' and not an actual null value.

2012-04-04 02:27
by Gohn67


0

Looks like your value is actually the string "null", not the value null. Ie

<?php
$x = "null";
$y = null;
var_dump($x);
var_dump($y);
?>

Output

string(4) "null"
NULL
2012-04-04 02:29
by RHSeeger