Original: PHP isset () and empty () the use of a different explanation
PHP's Isset () function is commonly used to detect whether a variable is set
Format: bool Isset (mixed var [, mixed Var [, ...])
Function: Detect whether the variable is set
return value:
Returns FALSE if the variable does not exist
Returns FALSE if the variable exists and its value is null
Returns TURE if the variable exists and the value is not NULL
Returns TRUE if each item meets the previous requirement when checking multiple variables at the same time, otherwise the result is FALSE
Version: PHP 3, PHP 4, PHP 5
More information:
After you use Unset () to release a variable, it will no longer be isset ().
PHP functions Isset () can only be used for variables, and passing any other parameter will result in parsing errors.
Detects if a constant is set to use the defined () function.
the Empty () function of PHP determines whether the value is null
Format: bool Empty (mixed Var)
function: Check if a variable is empty
return value:
Returns TRUE if the variable does not exist
If the variable exists and its value is "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any properties, the TURE is returned.
If the variable exists and the value is not "", 0, "0", NULL, FALSE, Array (), Var $var; And an object that does not have any properties, it returns FALSE
Version: PHP 3, PHP 4, PHP 5
More information:
The return value of empty () =! (Boolean) Var, but does not produce a warning message because the variable is undefined. See Convert to Boolean for more information.
Empty () can only be used for variables, and passing any other parameter will cause Paser error and terminate the run.
Detects if a constant is set to use the defined () function.
Example: A simple comparison of empty () with Isset ()
<? php $var = 0 // if (empty ( $var echo ' $var is either 0 or not set at all ' Span style= "color: #000000;" >; // The result is false because $var has set if (! Isset ( $var echo ' $var is not set at all ' ;} ? >
Note: Because this is a language structure and not a function, it cannot be called by a variable function.
Note: empty () detects only variables and detects anything that is non-variable will result in parsing errors. In other words, the statement behind will not work: Empty (Addslashes ($name)).
Here is a test of a isset and empty function Detailed example of the code, read this basically is almost:
<?PHPerror_reporting(E_all); Echo' <B> undefined $var</b><br> '; Echo"Isset Test:<br>"; if(isset($var )) { Echo' Variable $var exists!<br> ' ; } Echo"Empty Test:<br>"; if(Empty($var )){ Echo' The value of variable $var is null <Br> '; } Else { Echo' The value of variable $var is not null <Br> '; } Echo"Variable Direct test:<br>"; if($var ){ Echo' Variable $var exists!<br> '; } Else { Echo' Variable $var does not exist!<br> '; } Echo'----------------------------------<br> '; Echo' <B> $var = \ ' </b><Br> '; Echo"Isset Test:<br>"; $var= ' '; if(isset($var )) { Echo' Variable $var exists!<br> ' ; } Echo"Empty Test:<br>"; if(Empty($var )){ Echo' The value of variable $var is null <Br> '; } Else { Echo' The value of variable $var is not null <Br> '; } Echo"Variable Direct test:<br>"; if($var ){ Echo' Variable $var exists!<br> '; } Else { Echo' Variable $var does not exist!<br> '; } Echo'----------------------------------<br> '; Echo' <B> $var = 0</b><br> '; Echo' Isset test:<br> '; $var= 0 ; if(isset($var )) { Echo' Variable $var exists!<br> ' ; } Echo"Empty Test:<br>"; if(Empty($var )){ Echo' The value of variable $var is null <Br> '; } Else { Echo' The value of variable $var is not null <Br> '; } Echo"Variable Direct test:<br>"; if($var ){ Echo' Variable $var exists!<br> '; } Else { Echo' Variable $var does not exist!<br> '; } Echo'----------------------------------<br> '; Echo' <B> $var = Null</b><br> '; Echo' Isset test:<br> '; $var=NULL ; if(isset($var )) { Echo' Variable $var exists!<br> ' ; } Echo"Empty Test:<br>"; if(Empty($var )){ Echo' The value of variable $var is null <Br> '; } Else { Echo' The value of variable $var is not null <Br> '; } Echo"Variable Direct test:<br>"; if($var ){ Echo' Variable $var exists!<br> '; } Else { Echo' Variable $var does not exist!<br> '; } Echo'----------------------------------<br> '; Echo' <B> $var = ' php ' </b><Br> '; Echo' Isset test:<br> '; $var= "PHP"; if(isset($var )) { Echo' Variable $var exists!<br> ' ; } Echo"Empty Test:<br>"; if(Empty($var )){ Echo' The value of variable $var is null <Br> '; } Else { Echo' The value of variable $var is not null <Br> '; } Echo"Variable Direct test:<br>"; if($var ){ Echo' Variable $var exists!<br> '; } Else { Echo' Variable $var does not exist!<br> '; } ?>
When using PHP to write a page program, I often use the variable handler function to determine the PHP page trailing parameters of a variable value is empty, at the beginning I used to use the empty () function, but found some problems, so instead of using the isset () function, the problem is no longer.
As the name implies, empty () determines whether a variable is "empty", isset () determines whether a variable has been set. It is this so-called "as the name implies", so I began to take some detours: when a variable value equals 0 o'clock, empty () will also be set (True), so there will be some accidents. Originally, Empty () and isset () are variable-handler functions, which are used to determine whether variables have been configured, but they have a certain difference: Empty also detects whether the variable is null and zero. When a variable value of 0,empty () considers the variable equal to NULL, it is equivalent to no setting.
For example, detect $id variables, when $id = 0 o'clock, with empty () and isset () to detect whether the variable $id is configured, both will return a different value--empty () think that there is no configuration, isset () can get the value of $id: < /p>
$id =0; empty ( $id )? print "It's empty.": print "It's $id ." // result: It's empty. print "<br>" ; ! isset ( $id )? print "It's empty.": print "It's $id ." // result: It's 0.
This means that when we use the variable handler function, when the variable may appear with a value of 0, use empty () to be careful, it is wiser to replace it with Isset.
When the URL trailing parameter of a PHP page appears id=0 (for example: test.php?id=0), try comparing:
if (empty($id$id= 1;-id=0if (! Isset($id$id
The following code can be run separately to detect the above inference:
if (empty ( $id )) $id =1 print $id ; // get 1 if (! isset ( $id )) $id =1 print $id ; // get 0
The common denominator of
is that empty () and isset () are variable-handling functions that determine whether variables have been configured, because they have a great similarity in the process of processing variables, which leads to insufficient understanding of their relationships. From the two functions of empty () and isset () alone, it would make people more confused and take a different angle. Empty () and Isset () handle objects with no outside defined variables, 0, empty strings.
If the variable is 0, empty () returns True,isset () returns TRUE;&NBSP;
If the variable is an empty string, empty () returns True,isset () returns true;
If the variable is undefined, empty () returns True,isset () returns FLASE;&NBSP, and the Empty () in the
manual is interpreted as follows:
describes bool empty (mixed Var)
If Var is a non-null or nonzero value, empty () returns FALSE. In other words, "", 0, "0″, NULL, FALSE, Array (), Var $var; and objects that do not have any properties will be considered empty and return TRUE if Var is empty. Isset () is interpreted in the
manual as follows:
Isset () detects if the variable is set to &NBSP;
describes bool Isset (mixed var [, Mixed Var [, ...]] &NBSP
Returns TRUE if Var exists, otherwise FALSE. &NBSP
If a variable has been freed with unset (), it will no longer be isset (). If you use Isset () to test a variable that is set to NULL, FALSE is returned. Also be aware of a NULL byte ("?" ) is not equivalent to the NULL constant of PHP.
Warning: isset () can only be used for variables, because passing any other parameter will result in parsing errors. To detect if a constant is set, use the defined () function.
You can use the Isset function when you want to determine if a variable has been declared
When you want to determine whether a variable has been given data and is not empty, you can use the empty function
When you want to determine if a variable exists and is not empty first isset function again with the empty function
PHP isset () and empty () the difference between the use of a detailed