0

I'm trying to detect when an array element is null, but is_null isn't doing the job. In detail, I'm sending some POST data to a server, where the JavaScript object sent has no value:

$.ajax({
 type:     "POST",
 url:      "comms.php",
 data:     {practice:tab_content},   // array 'tab_content' has not been set
 dataType: "script"
});

In the server php code, I need to check whether 'practice' has a value (if it doesn't, I get an error when using foreach to cycle through the 'practice' array).

print_r($_POST) in the php code shows:

Array
(
   [practice] => null 
)

Here's my test program which checks what $_POST["practice"] actually is:

<?php
$params = "";
if(array_key_exists("practice", $_POST))
   $params .= "array_key_exists true;";
else
   $params .= "array_key_exists false;";

if(is_null($_POST["practice"]))
   $params .= " is_null true;";
else
   $params .= " is_null false;";

if(isset($_POST["practice"]))
   $params .= " isset true;";
else
   $params .= " isset false;";

if(empty($_POST["practice"]))
   $params .= " empty true;";
else
   $params .= " empty false;";

$params .= " value is '" . $_POST["practice"] . "'.";
$output  = "document.getElementById(\"demo\").innerHTML=\"";
$output .= $params;
$output .= "\";\n";
echo $output;
?>

The client output is identical on Opera and Chrome, and is:

array_key_exists true; is_null false; isset true; empty false; value is 'null'.

Any idea what's going on here? Thanks.

EDIT 22:10GMT

Thanks everybody - that's fixed it. Using var_dump did give the necessary information, as a couple of you suggested:

array(1) {
  ["practice"]=>
  string(4) "null"
}

And, if I modified the ajax call to send a string containing "null", then I got exactly the same effect. This is pretty strange, though - why does XMLHttpRequest send through a completely valid string containing n-u-l-l for an empty value? That makes it pretty difficult to send through a real n-u-l-l. Given this confusion, I'm using is_array, as benjam suggested. Thanks - love this site :)

5
  • what exactly is your question? im having a hard time understanding what you want to know Commented Dec 2, 2011 at 21:11
  • Everything passing with POST is a string. Even "null" Commented Dec 2, 2011 at 21:11
  • 1
    "value is 'null'" - That doesn't seem right to me. $foo = NULL; print "-$foo-"; prints --, not -null-. Commented Dec 2, 2011 at 21:11
  • Use var_dump instead of print_r to obtain more information about the value in question. Commented Dec 2, 2011 at 21:38
  • You can't sent a "real" null, because data is sent with application/x-www-form-urlencoded, which has no datatypes--all parameter names and values are strings. If you want to send a real null, you need to send JSON or some other format that has a concept of null. Commented Dec 2, 2011 at 22:56

3 Answers 3

5

Do a var_dump($_POST) instead, it shows you the varaible's type and size as well as content.

Basically anything coming out of _GET/_POST/_REQUEST is a string. A field which existed in the form, but had no data in it, will come out as an empty string. Your null value is not an actual php "null", it's a literal string whose value is n-u-l-l.

Given a php script like this:

<pre>
<?php

var_dump($_POST);

?>
</pre>

<form method="POST">
<input type="submit" name="submit">
<input type="hidden" name="hidden" value="">
</form>

clicking submit will get you this:

array(2) {
  ["submit"]=>
  string(12) "Submit Query"
  ["hidden"]=>
  string(0) ""
}

Note that the 'hidden' field exists, and has a 0-length string as its value. That means:

array_key_exists('hidden', $_POST); -> true
isset($_POST['hidden']); -> true
empty($_POST['hidden']); -> true
is_null($_POST['hidden']); -> false
Sign up to request clarification or add additional context in comments.

1 Comment

You can have arrays coming out of _GET/_POST/_REQUEST, but in this instance, you are correct. The value is a string literal "null". Checking is_array( ) will catch that, and return a false.
2

As @racar mentioned in a comment, if something is coming from the browser, then the data type is always string, except for array post values which will be an array of strings.

If you are seeing null, when you print it, that means it is literally a string containing the word "null".

So, for your example here, you need to be doing:

if ($_POST["practice"] == "null") {
  ...
}

Comments

1

Check for is_array($_POST["practice"]), that way, you will know it's an array, and foreach will never throw errors.

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.