In where shall I use isset() and !empty()
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."
isset() and empty() - what to use
It depends what you are looking for, if you are just looking to see if it is empty just use empty
as it checks whether it is set as well, if you want to know whether something is set or not use isset
.
Empty
checks if the variable is set and if it is it checks it for null, "", 0, etc
Isset
just checks if is it set, it could be anything not null
With empty
, the following things are considered empty:
- "" (an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- var $var; (a variable declared, but without a value in a class)
From http://php.net/manual/en/function.empty.php
As mentioned in the comments the lack of warning is also important with empty()
PHP Manual says
empty() is the opposite of (boolean) var, except that no warning is
generated when the variable is not set.
Regarding isset
PHP Manual says
isset() will return FALSE if testing a variable that has been set to NULL
Your code would be fine as:
<?php
$var = '23';
if (!empty($var)){
echo 'not empty';
}else{
echo 'is not set or empty';
}
?>
For example:
$var = "";
if(empty($var)) // true because "" is considered empty
{...}
if(isset($var)) //true because var is set
{...}
if(empty($otherVar)) //true because $otherVar is null
{...}
if(isset($otherVar)) //false because $otherVar is not set
{...}
What's the difference between 'isset()' and '!empty()' in PHP?
ISSET checks the variable to see if it has been set. In other words, it checks to see if the variable is any value except NULL or not assigned a value. ISSET
returns TRUE
if the variable exists and has a value other than NULL. That means variables assigned a "", 0, "0", or FALSE are set, and therefore are TRUE
for ISSET
.
EMPTY checks to see if a variable is empty. Empty is interpreted as: "" (an empty string), 0 (integer), 0.0 (float)`, "0" (string), NULL, FALSE, array() (an empty array), and "$var;" (a variable declared, but without a value in a class.
Why check both isset() and !empty()
This is completely redundant. empty
is more or less shorthand for !isset($foo) || !$foo
, and !empty
is analogous to isset($foo) && $foo
. I.e. empty
does the reverse thing of isset
plus an additional check for the truthiness of a value.
Or in other words, empty
is the same as !$foo
, but doesn't throw warnings if the variable doesn't exist. That's the main point of this function: do a boolean comparison without worrying about the variable being set.
The manual puts it like this:
empty()
is the opposite of(boolean) var
, except that no warning is generated when the variable is not set.
You can simply use !empty($vars[1])
here.
Is empty() enough or use isset()?
Op codes generated from isset($var) && !empty($var)
line # * op fetch ext return operands
---------------------------------------------------------------------------------
3 0 > EXT_STMT
1 ISSET_ISEMPTY_VAR 12800000 ~0 !0
2 > JMPZ_EX ~0 ~0, ->6
3 > ISSET_ISEMPTY_VAR 11800000 ~1 !0
4 BOOL_NOT ~2 ~1
5 BOOL ~0 ~2
6 > FREE ~0
4 7 > RETURN 1
and from !empty($var)
line # * op fetch ext return operands
---------------------------------------------------------------------------------
3 0 > EXT_STMT
1 ISSET_ISEMPTY_VAR 11800000 ~0 !0
2 BOOL_NOT ~1 ~0
3 FREE ~1
4 4 > RETURN 1
So yes it does repeat ISSET_ISEMPTY_VAR
but !$empty
. It depends on the value passed, but it it doesnt exist the top way is one opcode less BOOL_NOT
. but if it does exist, the bottom way is quicker.
Though its unlikely this is going to be a bottleneck in any application
How can I use PHP's isset() in addition to empty()?
Is this what you mean?
$pagesize = (isset($_GET['pagesize']) && !empty($_GET['pagesize'])) ?
$_GET['pagesize'] :
20;
http://us.php.net/manual/en/language.operators.logical.php
EDIT:
To be complete, empty already checks if something is set, so you don't need to use isset() as well.
I would also caution against using this code if it is going directly into a query or something similar. Consider using intval, is_numeric and similar functions.
In PHP when should you use isset?
If you don't use isset and the variable you're testing is undefined, you'll generate a warning. Additionally, there is a difference between something having never been set at all, and being set to an empty string, false, null or 0. Consider you're checking an array key and you just want to know if it's been created or not - and don't care about the value - if (!$a['key']) would return false if the key was 0, null or empty.
If you know the variable will be defined and the test only needs to know if the variable is non-false - that is 0, '', null, or false - then you can bypass isset.
Related Topics
Can You Store a Function in a PHP Array
Serializing and Submitting a Form With Jquery and PHP
Upgrading PHP in Xampp For Windows
Does MySQL_Real_Escape_String() Fully Protect Against SQL Injection
Session Data Lost in Chrome Only
Switch PHP Versions on Commandline Ubuntu 16.04
Find Total Number of Results in MySQL Query With Offset+Limit
Inserting Data to Table (MySQLi Insert)
Execute Raw SQL Using Doctrine 2
Laravel Migration: Unique Key Is Too Long, Even If Specified
Convert Utf8-Characters to Iso-88591 and Back in PHP
How to Delete Element With Domdocument
Remove Warning Messages in PHP
How to Best Configure PHP to Handle a Utf-8 Website
Notice: Array to String Conversion In