View Full Version : $_POST not empty
I am Abby
04-26-2010, 04:51 PM
Because I was posting a page to itself
I would first check to see if the item was set...
if (isset($_POST['iquote'])){
}
to keep the function from running when the page is refreshed I was told to...
if (isset($_POST['iquote])){
header('Location: '.$_SERVER['PHP_SELF'].'?iquote='.$iquote);
}
How do you handle it if someone clicks the submit button without entering any data?
if (isset($_POST['iquote'])) && $_POST['iquote'] <> empty(){
if( isset($_POST['iquote']) && !empty($_POST['iquote']) ){ /* do something */ }
djr33
04-26-2010, 05:24 PM
traq, is that needed? Isn't (!empty($x)===isset($x))? At least I believe that empty($x) will never return an error even if $x is not set.
I've never been sure about how PHP deals with variables that aren't set, but I've always used this:
if (isset($x)&&$x!='') {....
I've never had problems with it.
I am Abby
04-26-2010, 06:26 PM
Both ways seem to give the same results. Thanks boys!
djr33
04-26-2010, 07:15 PM
It appears that empty() is better than what I use.
if (!empty($x)) {...} should be all you need.
It checks both if there is a value and that that value is not 'blank' (empty string, 0, etc.).
traq, is that needed? Isn't (!empty($x)===isset($x))?
...
if (isset($x)&&$x!='') {....
...yes and no...
in most cases, they'll return the same answer. But (as I suspect you found below) a variable can be set (isset() returns TRUE) but be considered empty (e.g., an empty string - empty() returns TRUE).
Incidentally, $x != '' is also generally equivalent to !empty(), but:
The following things are considered to be empty:
* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)
It appears that empty() is better than what I use.
if (!empty($x)) {...} should be all you need.
It checks both if there is a value and that that value is not 'blank' (empty string, 0, etc.).
I don't know for sure if empty() makes isset() unneeded... for example, php.net says "...no warning is generated when the variable is not set", however, I don't know if that means empty differentiates between the two states. I suppose it's "close enough" in almost all cases (I can't quite think of an instance where it would matter, maybe if you're trying to do something with list() or extract()...).
Anyway, yeah.
djr33
04-27-2010, 03:59 AM
If the variable is not set, empty() returns TRUE; if the variable is set to any of the 'blank' values it also returns TRUE; otherwise it's false.
I don't think there's any case whatsoever where this is not the desired output or where it would be aided by isset().
Again, I've been doing it the long way for a while and always thought empty() was just a negative alias for isset(), so I'm glad I've now figured that out.
empty() is also closer to the correct value than $x!='' because the != operator DOES ignore type so that:
* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSEall are considered matches.
BUT it would not match an empty array.
However, an empty array is in some sense "set" so that's possible.
I expect an existing but not-set variable ( var $var; ) would return a match using !='' also.
Note: to specify a certain type, use !==, so !=='' means "not exactly an empty string" and !==FALSE means "not exactly false" and !==0 means "not exactly a number equal to 0" etc. But with only != it ignores type. The 2 character operator (!= or ==) is type-ignoring and the 3 character operator (!== or ===) is type-specific.
...
I don't think there's any case whatsoever where this is not the desired output or where it would be aided by isset().
I can't think of anything either, but it still sticks in my head as significant for some reason.
In any case, yes, I agree that if(!empty($x)) is fine in all practical respects, and really doesn't need the isset() check alongside it.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.