3

Sol let's suppose that we send a post query to a PHP file, this query where we have ...&title=title where title is for example title=$(#title).val(); (assuming we're including Jquery) in PHP we have $title=$_POST['title']; Let's suppose then that title is null , will PHP consider null as string ? or something empty ?

6
  • Nulls are nulls everywhere. Isn't it? Commented Sep 29, 2013 at 15:57
  • @xy_: You can not tell a variable is null in php, with the exception of the actual 'value' NULL. Trying to set it with any other null-like value will result in an empty string Commented Sep 29, 2013 at 15:59
  • 2
    title won't be null, val() always returns a string (assuming you've selected an input) Commented Sep 29, 2013 at 15:59
  • Then You just don write anything for the title, It'll give undefined , bu then how will php understand undefined ? will $title get "undefined" String as value ? Commented Sep 29, 2013 at 16:02
  • I think you mean "interpret" not "compile" Commented Sep 29, 2013 at 16:02

4 Answers 4

4

From the PHP Manual:

The special NULL value represents a variable with no value. NULL is the only possible value of type NULL.

A variable is considered to be NULL if:

  • it has been assigned the constant NULL.
  • it has not been set to any value yet.
  • it has been unset().

"NULL" is not the same as NULL.

var_dump("null" == NULL);

Outputs:

bool(false)
Sign up to request clarification or add additional context in comments.

6 Comments

But the thing is this null value is sent from Javascript , it's like title=null then you do a $title=$_POST['title']; which basically gives $title="null"; doesn't it ?
that is will be like this: $variable = "null";, null as a string
@AymaneShuichi: See the answer. "NULL" is not the same as NULL
@Downvoter: can you suggest any improvements / explain the downvote?
It would not evaluate to the type NULL, but more a string containing 'null', but in this case, the argument is most likely parsed, but with no value.
|
2

The title gets converted to a string before sending it to the server.

Converting the JS value null to a string results in a string "null". Therefore, PHP will only interpret the variable as a string.

However, I doubt that you will receive a null value from an empty input box. You will get an empty string instead.

1 Comment

$(#title).val(); will probably not evaluate to 'null' unless the element doesn't exist.
1

In your case, if you send a $(#title).val(); as data in an ajax request to a php script, and the input is empty, it would just be an empty string.

1 Comment

what if it returns undefined in Javascript ? will $title get the string "undefined" ?
0

php intercepts js null as "null" -> string, So to handle form request, i had small work around in my php code like below

function checknull($arg){
    if($arg == "null"){
      return;
    }else{
        return $arg;
    }
}
$a=array();
$b = "null";
$a['hi'] = checknull($b);
var_dump($a);

This will output as

array(1) { ["hi"]=> NULL }

//same you can test it for $_REQUEST

$var1 = checknull($_POST['title']);
var_dump($var1);

or you can use

$var1 = $_POST['title'] === 'null'? NULL : $_POST['title'];
var_dump($var1);

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.