PHP isset () and empty () the application of the difference is detailed

Source: Internet
Author: User
Tags bbcode php language
PHP isset () and empty () the difference between the use of a detailed
Reference

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 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: Empty () and isset () a simple comparison of the copy Code code is as follows:
 
  

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 the code code as follows:
 $var Not defined
'; echo "Isset test:
"; if (Isset ($var)) {echo ' variable $var exists!
' ; } echo "Empty test:
"; if (empty ($var)) {echo ' variable $var value is empty
'; The value of the else {echo ' variable $var is not empty
'; } echo "Variable direct test:
"; if ($var) {echo ' variable $var exists!
'; } else {echo ' variable $var does not exist!
'; } Echo '----------------------------------
'; Echo '$var = \ ' \ '
'; echo "Isset test:
"; $var = "; if (Isset ($var)) {echo ' variable $var exists!
' ; } echo "Empty test:
"; if (empty ($var)) {echo ' variable $var value is empty
'; The value of the else {echo ' variable $var is not empty
'; } echo "Variable direct test:
"; if ($var) {echo ' variable $var exists!
'; } else {echo ' variable $var does not exist!
'; } Echo '----------------------------------
'; Echo '$var = 0
'; Echo ' Isset test:
'; $var = 0; if (Isset ($var)) {echo ' variable $var exists!
' ; } echo "Empty test:
"; if (empty ($var)) {echo ' variable $var value is empty
'; The value of the else {echo ' variable $var is not empty
'; } echo "Variable direct test:
"; if ($var) {echo ' variable $var exists!
'; } else {echo ' variable $var does not exist!
'; } Echo '----------------------------------
'; Echo '$var = null
'; Echo ' Isset test:
'; $var = null; if (Isset ($var)) {echo ' variable $var exists!
' ; } echo "Empty test:
"; if (empty ($var)) {echo ' variable $var value is empty
'; The value of the else {echo ' variable $var is not empty
'; } echo "Variable direct test:
"; if ($var) {echo ' variable $var exists!
'; } else {echo ' variable $var does not exist!
'; } Echo '----------------------------------
'; Echo '$var = "php"
'; Echo ' Isset test:
'; $var = "PHP"; if (Isset ($var)) {echo ' variable $var exists!
' ; } echo "Empty test:
"; if (empty ($var)) {echo ' variable $var value is empty
'; The value of the else {echo ' variable $var is not empty
'; } echo "Variable direct test:
"; if ($var) {echo ' variable $var exists!
'; } else {echo ' variable $var does not exist!
'; }?>
  • 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.