Which I feel is crazy and incredibly vauge. 0 and null are not the same. 0 is a value. null isn't.Coco wrote:(tho empty returns true if the value is 0 OR null)
Why use isset()
Moderator: General Moderators
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
Well, I rely on globals anywaynielsene wrote:Using that code, you will always "do something" and never "do something else"evilmonkey wrote:Code: Select all
$var=$_GET['somevar']; if (isset($var)){ //do something } else { //do something else }
$var is always set (its value might be empty, but....)
You would want to doi.e. you can't assign to a variable and expect it to be unset.....Code: Select all
if (isset($_GET['somevar']))
That's why this thread can be summed up as:McGruff wrote:Also, and rather strangely, $var = '0'; is held to be empty by php as well as $var = 0 ..Coco wrote:(tho empty returns true if the value is 0 OR null)
1) Decide if you need to know if a variable is set or empty first.
2) If you just need to know if it's empty, decide what you think empty is.
3) Then test.
With checking forms, you don't allways know if the bloke on the other side entered something into a field. It's better to see if the var is set first, but even this isn't enough. What I've take to doing is something like the below...
Code: Select all
if( isset($_POST['blokes_name']) && !empty($_POST['blokes_name']) )
{ /* Do somethin' about it! */ }See how the logic starts getting convoluted?
Cheers,
BDKR