limitphp Posted January 23, 2009 Share Posted January 23, 2009 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 Quote Link to comment Share on other sites More sharing options...
rubing Posted January 23, 2009 Share Posted January 23, 2009 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 ); Quote Link to comment Share on other sites More sharing options...
justinh Posted January 23, 2009 Share Posted January 23, 2009 <?php $var = ""; if(is_null($var)){ echo "variable is NULL"; }else{ echo "variable is not NULL"; } ?> outputs "variable is not NULL". Quote Link to comment Share on other sites More sharing options...
rubing Posted January 23, 2009 Share Posted January 23, 2009 good work justin! you are quite the php detective. Quote Link to comment Share on other sites More sharing options...
trq Posted January 23, 2009 Share Posted January 23, 2009 <?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. Quote Link to comment Share on other sites More sharing options...
justinh Posted January 23, 2009 Share Posted January 23, 2009 Just trying to help with the little knowledge I do have of PHP coding Quote Link to comment Share on other sites More sharing options...
rubing Posted January 23, 2009 Share Posted January 23, 2009 So, justin what do you think is happening with the empty POST variable? and how could we test that? Quote Link to comment Share on other sites More sharing options...
justinh Posted January 23, 2009 Share Posted January 23, 2009 <?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. Quote Link to comment Share on other sites More sharing options...
rubing Posted January 23, 2009 Share Posted January 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
justinh Posted January 23, 2009 Share Posted January 23, 2009 Okay, I'll throw together something, and post it. Quote Link to comment Share on other sites More sharing options...
rubing Posted January 23, 2009 Share Posted January 23, 2009 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! Quote Link to comment Share on other sites More sharing options...
bluesoul Posted January 24, 2009 Share Posted January 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
rubing Posted January 24, 2009 Share Posted January 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
uniflare Posted January 24, 2009 Share Posted January 24, 2009 var_dump would of been easier . you should always know wether a variable is null or just empty like so: if(isset($variable) && $variable != ""){ echo("Blank"); } Quote Link to comment Share on other sites More sharing options...
rubing Posted January 24, 2009 Share Posted January 24, 2009 gee...where were you when it mattered ??? Quote Link to comment Share on other sites More sharing options...
uniflare Posted January 24, 2009 Share Posted January 24, 2009 lol at a friends house, sorry Quote Link to comment Share on other sites More sharing options...
uniflare Posted January 24, 2009 Share Posted January 24, 2009 Sorry this is wrong: var_dump would of been easier . 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 . you should always know wether a variable is null or just empty like so: if(isset($variable) && $variable == ""){ echo("Blank"); } Quote Link to comment Share on other sites More sharing options...
limitphp Posted January 24, 2009 Author Share Posted January 24, 2009 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.