Jump to content

[SOLVED] Difference bewteen nothing and null?


limitphp

Recommended Posts

I have:

$new_password = check_input($_POST['new_password']);

 

But what if, they leave the textbox "new_password" blank?

I still run the above code....

I'm curious though, it appears that when they leave new_password blank, $new_password is not = ""

Rather it seems to be = null

 

What is the difference between "" and null and why does that happen?

 

Thanks

Link to comment
Share on other sites

I dont' think an empty string is considered NULL.  I think rather that an empty POST value as translated as NULL. 

 

Why don't you experment and report back to us, I would like to know. 

 

try setting  a variable as empty:

 

$var = "";

 

then test if it's null:

 

is_null  ( $var  );

Link to comment
Share on other sites

 
<?php

$var = "";

if(is_null($var)){

   echo "variable is NULL";
   
   }else{
   
   echo "variable is not NULL";
   
   }


?>

 

outputs "variable is not NULL".

 

This is because you assigned an empty string to it. A variable is only considered NULL when it has been assigned the constant NULL, has not been assigned any value or has been unset.

Link to comment
Share on other sites

<?php
$_POST['new_password'] = NULL;

$_POST['new_password2'] = "NULL";




if(is_null($_POST['new_password'])){

echo "new_password is null!<br />";

}else{

echo "new_password is not null!<br />";

}

if(is_null($_POST['new_password2'])){

  echo "new_password2 is null!<br />";

}else{

echo "new_password2 is not null!<br />";

}

?>

 

output is

new_password is null!
new_password2 is not null!

 

Don't know if this helps you or not, but I just thought I would post it.

Link to comment
Share on other sites

hmmm....that's not really what i was trying to get at.  The OT wanted to know why his POST variable was coming up NULL.  So, he asked us (very lazy) whether php considers an empty string to be null.  We proved otherwise, by simply trying it out.  So, now I am wondering does the browser post an empty string as NULL?  Or does php unset the empty POST variable? 

 

It's a very esoteric question, which really has no value.  Other than to help me procrastinate from writing my thesis. 

 

 

Link to comment
Share on other sites

On the php side you just need to say

 

print_r($POST);

 

 

and then we can try using a form like the OT and then maybe curl as well. of course this may not answer our question, but would be fairly easy.

 

After that we may need to use a tool like Live HTTP headers in order to see what the browser is outputting.  Anyways, I have to go to the gym and then eat, so will be back later to see your result...  Good luck!

 

Link to comment
Share on other sites

hmmm....that's not really what i was trying to get at.  The OT wanted to know why his POST variable was coming up NULL.  So, he asked us (very lazy) whether php considers an empty string to be null.  We proved otherwise, by simply trying it out.  So, now I am wondering does the browser post an empty string as NULL?  Or does php unset the empty POST variable? 

 

It's a very esoteric question, which really has no value.  Other than to help me procrastinate from writing my thesis.

 

In my experience a POSTed value left blank will still be a variable with a pointer and physical address, with a null value (\0).  This is why sometimes checking the strlen() of a POSTed variable is the easiest way to determine if operations need to be performed on it.

 

I tend to only use isset() on GET vars for a centralized page, e.g. if ?do=foo execute this block, if ?do=bar execute that block, if ?do isn't set show the index.

Link to comment
Share on other sites

I guess that answers the question.

 

To summarize:

 

1. Posting a variable with an empty string, results in NULL value

2. Setting a variable as empty string does not result in NULL value

 

How marvelous!  What a wonderful php adventure that was.  I hope you learned something folks.

Link to comment
Share on other sites

Sorry this is wrong:

var_dump would of been easier :P.

 

you should always know wether a variable is null or just empty like so:

 

if(isset($variable) && $variable != ""){

  echo("Blank");

}

 

should be:

 

var_dump would of been easier :P.

 

you should always know wether a variable is null or just empty like so:

 

if(isset($variable) && $variable == ""){

  echo("Blank");

}

Link to comment
Share on other sites

thanks for the input folks....

 

I think, according to thorpe, if you do not set a value to a variable, it is null.

 

And if you set any value to a variable including ="", it is not null.

 

I was basically, trying to check to see if a user changed their password....

so I did if ($newpassword!="")

{

yes they changed it

 

but that didn't work...for the reasons stated above.....

 

so i had to change it to

if ($newpassword!=null)

{

yes they changed it

 

that seemed to work.

 

thanks guys...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.