


Detailed explanation of the difference between isset() and empty() in php
Through learning the PHP language, you should know that it is a function-based HTML scripting language. A huge function library supports the implementation of PHP language functions. Below we will introduce to you the related usage of PHP functions isset() and empty().
PHP’s isset() function is generally used to detect whether a variable is set
Format: bool isset (mixed var [, mixed var [, ...]])
Function: detect whether a variable is set
Return value:
If the variable does not exist, it returns FALSE
If the variable exists and its value is NULL, it also returns FALSE
If the variable exists and its value is not NULL, it returns TRUE
When checking multiple variables at the same time, each single item matches the previous one Returns TRUE only when requested, otherwise the result is FALSE
Versions: PHP 3, PHP 4, PHP 5
More instructions:
After using unset() to release a variable, it will no longer be isset().
PHP function isset() can only be used for variables, passing any other parameters will cause parsing errors.
To detect whether a constant has been set, use the defined() function.
PHP's empty() function determines whether the value is empty
Format: bool empty (mixed var)
Function: Check whether a variable is empty
Return value:
If the variable does not exist, it returns TRUE
If If the variable exists and its value is "", 0, "0", NULL, FALSE, array(), var $var; and an object without any attributes, TRUE is returned. If the variable exists and its value is not "", 0 , "0", NULL, FALSE, array(), var $var; and objects without any attributes, return FALSE
Versions: PHP 3, PHP 4, PHP 5
empty() can only be used for variables. Passing any other parameters will cause a Paser error and terminate the operation.
To detect whether a constant has been set, use the defined() function.
<?php $var = 0; // 结果为 true,因为 $var 为空 if (empty($var)) { echo '$var is either 0 or not set at all'; } // 结果为 false,因为 $var 已设置 if (!isset($var)) { echo '$var is not set at all'; } ?>
Note: Since this is a language structure rather than a function, it cannot be called by variable functions.
Note: empty() only detects variables, detecting anything that is not a variable will result in a parsing error. In other words, the following statement will not work: empty(addslashes($name)).The following is a code for a detailed example of isset and empty functions that we have tested. After reading this, it is basically the same:
<?php error_reporting(E_ALL); echo '<B>未定义$var</b><Br>'; echo "isset测试:<Br>"; if ( isset ( $var )) { echo '变量$var存在!<Br>' ; } echo "empty测试:<Br>"; if ( empty ( $var )){ echo '变量$var的值为空<Br>'; } else { echo '变量$var的值不为空<Br>'; } echo "变量直接测试:<Br>"; if ( $var ){ echo '变量$var存在!<Br>'; } else { echo '变量$var不存在!<Br>'; } echo '----------------------------------<br>'; echo '<B>$var = \'\'</b><Br>'; echo "isset测试:<Br>"; $var = ''; if ( isset ( $var )) { echo '变量$var存在!<Br>' ; } echo "empty测试:<Br>"; if ( empty ( $var )){ echo '变量$var的值为空<Br>'; } else { echo '变量$var的值不为空<Br>'; } echo "变量直接测试:<Br>"; if ( $var ){ echo '变量$var存在!<Br>'; } else { echo '变量$var不存在!<Br>'; } echo '----------------------------------<br>'; echo '<B>$var = 0</b><Br>'; echo 'isset测试:<Br>'; $var = 0 ; if ( isset ( $var )) { echo '变量$var存在!<Br>' ; } echo "empty测试:<Br>"; if ( empty ( $var )){ echo '变量$var的值为空<Br>'; } else { echo '变量$var的值不为空<Br>'; } echo "变量直接测试:<Br>"; if ( $var ){ echo '变量$var存在!<Br>'; } else { echo '变量$var不存在!<Br>'; } echo '----------------------------------<br>'; echo '<B>$var = null</b><Br>'; echo 'isset测试:<Br>'; $var = null ; if ( isset ( $var )) { echo '变量$var存在!<Br>' ; } echo "empty测试:<Br>"; if ( empty ( $var )){ echo '变量$var的值为空<Br>'; } else { echo '变量$var的值不为空<Br>'; } echo "变量直接测试:<Br>"; if ( $var ){ echo '变量$var存在!<Br>'; } else { echo '变量$var不存在!<Br>'; } echo '----------------------------------<br>'; echo '<B>$var ="php"</b><Br>'; echo 'isset测试:<Br>'; $var = "php"; if ( isset ( $var )) { echo '变量$var存在!<Br>' ; } echo "empty测试:<Br>"; if ( empty ( $var )){ echo '变量$var的值为空<Br>'; } else { echo '变量$var的值不为空<Br>'; } echo "变量直接测试:<Br>"; if ( $var ){ echo '变量$var存在!<Br>'; } else { echo '变量$var不存在!<Br>'; } ?>
$id=0; empty($id)?print "It's empty .":print "It's $id ."; //结果:It's empty . print "<br>"; !isset($id)?print "It's empty .":print "It's $id ."; //结果:It's 0 .
This means that when we use the variable processing function, when the variable may have a value of 0, be careful when using empty(). It is more sensible to replace it with isset at this time.
When the URL tail parameter of a php page appears id=0 (for example: test.php?id=0), try to compare:if(empty($id)) $id=1; - 若 id=0 ,id 也会为1 if(!isset($id)) $id=1; - 若 id=0 ,id 不会为1
You can run the following code separately to detect the above inference:
if(empty($id)) $id=1; print $id; // 得到 1 if(!isset($id)) $id=1; print $id; //得到 0
If the variable is not Definition, empty() will return TRUE, isset() will return FLASE;
If var is a non-empty or non-zero value, then empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var; and objects without any properties will be considered empty, and TRUE will be returned if var is empty.
If a variable has been released using unset(), it will no longer be isset(). If you use isset() to test a variable that is set to NULL, it will return FALSE. Also note that a NULL byte ("?") is not equivalent to PHP's NULL constant.
Warning: isset() can only be used with variables, as passing any other parameters will cause a parsing error. If you want to check whether a constant has been set, you can use the defined() function.
When you want to judge whether a variable has been declared, you can use the isset function.
When you want to judge whether a variable has been assigned data and is not empty, you can use the empty function.
When you want to judge whether a variable exists and is not empty, use the isset function first and then use it. empty function

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
