PHP isset () and empty () the use of a different explanation (go)

Source: Internet
Author: User
Tags php form php form validation

By learning the language of PHP, you should know that it is a function-based HTML scripting language. The large library of functions supports the implementation of PHP language functions. Let's introduce the relevant usage of PHP function isset () and empty (). 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 exists and its value is NULL, and returns FALSE if the variable exists and the value is not NULL, returns TRUE if each item conforms to the previous requirement when the TURE check multiple variables at the same time, otherwise the result is false
Version: PHP 3, PHP 4, PHP 5 more information: After using unset () to release the 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 Description: Empty () return value =! (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 ()Copy CodeThe code is as follows: <?php $var = 0; The result is true because the $var is empty if (empty ($var)) {echo ' $var are either 0 or not set at all ';
}//The result is false because the $var has set if (!isset ($var)) {echo ' $var is not set to 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)).
Below is the code for a detailed example of the isset and empty functions that have been tested by the script house, which is basically the same:Copy CodeThe code is as follows: <?php error_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 ' variable $var value is null <Br> ';
The value of the else {echo ' 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 ' variable $var value is null <Br> ';
The value of the else {echo ' 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 ' variable $var value is null <Br> ';
The value of the else {echo ' 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 ' variable $var value is null <Br> ';
The value of the else {echo ' 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 ' variable $var value is null <Br> ';} else {echo ' variable $var value 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 tail 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" and 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, the detection of $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:Copy CodeThe code is as follows: $id = 0; Empty ($id)? print "It ' s empty.":p rint "it's $id."; Result: It ' s empty. print "<br>";
!isset ($id)? print "It ' s empty.":p rint "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:Copy CodeThe code is as follows: if (empty ($id)) $id = 1; -If id=0, the ID will also be 1
if (!isset ($id)) $id = 1; -If id=0, the ID will not be 1 separate run the following code to detect the above inference:Copy CodeThe code is as follows: if (empty ($id)) $id = 1; Print $id; Get 1 if (!isset ($id)) $id = 1; Print $id; Get 0 To say their connection, the common point is that empty () and isset () are variable processing functions, the role is to determine whether the variables have been configured, it is because they have a large similarity in the process of variables, it leads to lack of understanding of their relationship. 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 () to return true;
If the variable is an empty string, empty () returns True,isset () to return true;
If the variable is undefined, empty () returns True,isset () returns flase;
empty () is explained in the manual as follows:
description 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.
the Isset () is explained in the manual as follows:
Isset () detects if the variable is set
description bool Isset (mixed var [, mixed Var [, ...])
Returns TRUE if Var exists, otherwise FALSE is returned.
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 whether a variable has been declared or not, when you want to determine whether a variable has been given data and 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 Articles you may be interested in:
    • In-depth example testing of PHP empty (), Isset (), Is_null ()
    • The difference of Is_null,empty,isset,unset in PHP is described in detail
    • How to use PHP form validation 3 functions isset (), Empty (), Is_numeric ()
    • PHP empty,isset,is_null judgment Comparison (differences and similarities)
    • A detailed description of the difference between PHP empty () and isset ()

Transfer from http://www.jb51.net/article/24666.htm

PHP isset () and empty () the use of a different explanation (go)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.