In where shall I use isset() and !empty()
Solution 1:
isset vs. !empty
FTA:
"isset() checks if a variable has a value including (False, 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."
Solution 2:
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
.