tags:

views:

513

answers:

6

I read somewhere that isset() function is that an empty string tests as TRUE, os isset() is not an effective way to validate text inputs and text boxes from a HTML form. So you can use empty() to check that a user typed something....

Q1. Is it true that isset() function treat an empty string as TRUE?

Q2. Then in which situation I should use isset()? Should I always use !empty() to check if there is something?

For example instead of if(isset($_GET['gender']))...

Using this if(!empty($_GET['gender']))...

Thanks for your help.

+8  A: 

isset vs. !empty

FTA:

"Isset() checks if a variable has a value including ( Flase , 0 , or Empty string) , But not NULL. Returns TRUE if var exists; FALSE otherwise.

On the other hand the empty() function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value."

dassouki
+1  A: 

Using empty is enough:

if(!empty($variable)){
    // Do stuff
}

Additionally, if you want an integer value it might also be worth checking that intval($variable) !== FALSE.

You
The isset is redundant.
orlandu63
@orlandu63: Edited, fixed.
You
Also, intval() never returns FALSE.
Josh Davis
+2  A: 

In the most general way :

  • isset tests if a variable (or an element of an array, or a property of an object) exists (and is not null)
  • empty tests if a variable (...) contains some non-empty data.


To answer question 1 :

$str = '';
var_dump(isset($str));

gives

boolean true

Because the variable $str exists.


And question 2 :

You should use isset to determine whether a variable exists ; for instance, if you are getting some data as an array, you might need to check if a key isset in that array.
Think about $_GET / $_POST, for instance.

Now, to work on its value, when you know there is such a value : that is the job of empty.

Pascal MARTIN
A: 
isset($variable) === (@$variable !== null)
empty($variable) === (@$variable == false)
orlandu63
+3  A: 

isset and empty are intended to be used only for variables and not just values. So isset("foobar") or empty("") will raise errors.

So your first question should rather be if isset returns true for a variable that holds an empty string. And the answer is:

$var = "";
var_dump(isset($var));

The type comparison tables in PHP’s manual is quite handy for such questions.

isset basically checks if a variable has any value other than null since non-existing variables have always the value null. empty is kind of the counter part to isset but does also treat the integer value 0 and the string value "0" as empty. (Again, take a look at the type comparison tables.)

Gumbo
A: 

isset() is not an effective way to validate text inputs and text boxes from a HTML form

You can rewrite that as "isset() is not a way to validate input." To validate input, use PHP's filter extension. filter_has_var() will tell you whether the variable exists while filter_input() will actually filter and/or sanitize the input.

Note that you don't have to use filter_has_var() prior to filter_input() and if you ask for a variable that is not set, filter_input() will simply return null.

Josh Davis