search
HomeBackend DevelopmentPHP TutorialDetailed explanation of the difference between PHP 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 Then return 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, TRUE will be returned only when each single item meets the previous requirement. 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 a parsing error.
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, return TRUE
If the variable exists and its value For "", 0, "0", NULL,, FALSE, array(), var $var; and objects without any attributes, TRUE is returned
If the variable exists and the value is not "", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes will return FALSE
Versions: PHP 3, PHP 4, PHP 5
More instructions:
empty() return value =!(boolean ) var, but no warning message will be generated because the variable is undefined. See Converting to Boolean for more information.
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.
Example: A simple comparison of empty() and isset()

<span>php 
</span><span>$var</span> = 0<span>; 
</span><span>//</span><span> 结果为 true,因为 $var 为空 </span><span>if</span> (<span>empty</span>(<span>$var</span><span>)) { 
</span><span>echo</span> '$var is either 0 or not set at all'<span>; 
} 
</span><span>//</span><span> 结果为 false,因为 $var 已设置 </span><span>if</span> (!<span>isset</span>(<span>$var</span><span>)) { 
</span><span>echo</span> '$var is not set at all'<span>; 
} 
</span>?> 

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 tested code of a detailed example of isset and empty functions. After reading this, it is basically the same:

<span>php 
</span><span>error_reporting</span>(<span>E_ALL</span><span>); 
</span><span>echo</span> '<b>未定义$var</b><br>'<span>; 
</span><span>echo</span> "isset测试:<br>"<span>; 
</span><span>if</span> ( <span>isset</span> ( <span>$var</span><span> )) 
{ 
</span><span>echo</span> '变量$var存在!<br>'<span> ; 
} 
</span><span>echo</span> "empty测试:<br>"<span>; 
</span><span>if</span> ( <span>empty</span> ( <span>$var</span><span> )){ 
</span><span>echo</span> '变量$var的值为空<br>'<span>; 
} 
</span><span>else</span><span>
{ 
</span><span>echo</span> '变量$var的值不为空<br>'<span>; 
} 
</span><span>echo</span> "变量直接测试:<br>"<span>; 
</span><span>if</span> ( <span>$var</span><span> ){ 
</span><span>echo</span> '变量$var存在!<br>'<span>; 
} 
</span><span>else</span><span> { 
</span><span>echo</span> '变量$var不存在!<br>'<span>; 
} 
</span><span>echo</span> '----------------------------------<br>'<span>; 
</span><span>echo</span> '<b>$var = \'\'</b><br>'<span>; 
</span><span>echo</span> "isset测试:<br>"<span>; 
</span><span>$var</span> = ''<span>; 
</span><span>if</span> ( <span>isset</span> ( <span>$var</span><span> )) 
{ 
</span><span>echo</span> '变量$var存在!<br>'<span> ; 
} 
</span><span>echo</span> "empty测试:<br>"<span>; 
</span><span>if</span> ( <span>empty</span> ( <span>$var</span><span> )){ 
</span><span>echo</span> '变量$var的值为空<br>'<span>; 
} 
</span><span>else</span><span>
{ 
</span><span>echo</span> '变量$var的值不为空<br>'<span>; 
} 
</span><span>echo</span> "变量直接测试:<br>"<span>; 
</span><span>if</span> ( <span>$var</span><span> ){ 
</span><span>echo</span> '变量$var存在!<br>'<span>; 
} 
</span><span>else</span><span> { 
</span><span>echo</span> '变量$var不存在!<br>'<span>; 
} 
</span><span>echo</span> '----------------------------------<br>'<span>; 
</span><span>echo</span> '<b>$var = 0</b><br>'<span>; 
</span><span>echo</span> 'isset测试:<br>'<span>; 
</span><span>$var</span> = 0<span> ; 
</span><span>if</span> ( <span>isset</span> ( <span>$var</span><span> )) 
{ 
</span><span>echo</span> '变量$var存在!<br>'<span> ; 
} 
</span><span>echo</span> "empty测试:<br>"<span>; 
</span><span>if</span> ( <span>empty</span> ( <span>$var</span><span> )){ 
</span><span>echo</span> '变量$var的值为空<br>'<span>; 
} 
</span><span>else</span><span>
{ 
</span><span>echo</span> '变量$var的值不为空<br>'<span>; 
} 
</span><span>echo</span> "变量直接测试:<br>"<span>; 
</span><span>if</span> ( <span>$var</span><span> ){ 
</span><span>echo</span> '变量$var存在!<br>'<span>; 
} 
</span><span>else</span><span> { 
</span><span>echo</span> '变量$var不存在!<br>'<span>; 
} 
</span><span>echo</span> '----------------------------------<br>'<span>; 
</span><span>echo</span> '<b>$var = null</b><br>'<span>; 
</span><span>echo</span> 'isset测试:<br>'<span>; 
</span><span>$var</span> = <span>null</span><span> ; 
</span><span>if</span> ( <span>isset</span> ( <span>$var</span><span> )) 
{ 
</span><span>echo</span> '变量$var存在!<br>'<span> ; 
} 
</span><span>echo</span> "empty测试:<br>"<span>; 
</span><span>if</span> ( <span>empty</span> ( <span>$var</span><span> )){ 
</span><span>echo</span> '变量$var的值为空<br>'<span>; 
} 
</span><span>else</span><span>
{ 
</span><span>echo</span> '变量$var的值不为空<br>'<span>; 
} 
</span><span>echo</span> "变量直接测试:<br>"<span>; 
</span><span>if</span> ( <span>$var</span><span> ){ 
</span><span>echo</span> '变量$var存在!<br>'<span>; 
} 
</span><span>else</span><span> { 
</span><span>echo</span> '变量$var不存在!<br>'<span>; 
} 
</span><span>echo</span> '----------------------------------<br>'<span>; 

</span><span>echo</span> '<b>$var ="php"</b><br>'<span>; 
</span><span>echo</span> 'isset测试:<br>'<span>; 
</span><span>$var</span> = "php"<span>; 
</span><span>if</span> ( <span>isset</span> ( <span>$var</span><span> )) 
{ 
</span><span>echo</span> '变量$var存在!<br>'<span> ; 
} 

</span><span>echo</span> "empty测试:<br>"<span>; 
</span><span>if</span> ( <span>empty</span> ( <span>$var</span><span> )){ 
</span><span>echo</span> '变量$var的值为空<br>'<span>; 
} 
</span><span>else</span><span>
{ 
</span><span>echo</span> '变量$var的值不为空<br>'<span>; 
} 
</span><span>echo</span> "变量直接测试:<br>"<span>; 
</span><span>if</span> ( <span>$var</span><span> ){ 
</span><span>echo</span> '变量$var存在!<br>'<span>; 
} 
</span><span>else</span><span> { 
</span><span>echo</span> '变量$var不存在!<br>'<span>; 
} 
</span>?>

When using PHP to write page programs, I often use variable processing functions to determine the PHP page tail parameters Whether the value of a certain variable is empty. At the beginning, I was used to using the empty() function, but found some problems, so I switched to the isset() function and the problem no longer existed.
As the name suggests, 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" that made me take some detours at the beginning: when a variable value is equal to 0, empty() will also be true (True), so some accidents will occur. It turns out that although empty() and isset() are both variable processing functions, they are both used to determine whether the variable has been configured, but they have certain differences: empty will also detect whether the variable is empty or zero. When a variable value is 0, empty() considers the variable to be equivalent to being empty, which is equivalent to not being set.
For example, to detect the $id variable, when $id=0, use empty() and isset() to detect whether the variable $id has been configured. Both will return different values ​​- empty() considers it not configured, isset() Able to get the value of $id:

<span>$id</span>=0<span>; 
</span><span>empty</span>(<span>$id</span>)?<span>print</span> "It's empty .":<span>print</span> "It's <span>$id</span> ."<span>; 
</span><span>//</span><span>结果:It's empty . </span><span>print</span> "<br>"<span>; 
</span>!<span>isset</span>(<span>$id</span>)?<span>print</span> "It's empty .":<span>print</span> "It's <span>$id</span> ."<span>; 
</span><span>//</span><span>结果:It's 0 . </span>

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:

<span>if</span>(<span>empty</span>(<span>$id</span>)) <span>$id</span>=1; - 若 id=0<span> ,id 也会为1 
</span><span>if</span>(!<span>isset</span>(<span>$id</span>)) <span>$id</span>=1; - 若 id=0 ,id 不会为1 

You can run the following code separately to detect the above inference:

<span>if</span>(<span>empty</span>(<span>$id</span>)) <span>$id</span>=1<span>; 
</span><span>print</span><span>$id</span>; <span>//</span><span> 得到 1 </span><span>if</span>(!<span>isset</span>(<span>$id</span>)) <span>$id</span>=1<span>; 
</span><span>print</span><span>$id</span>; <span>//</span><span>得到 0 </span>


To talk about their connection, their common point is that empty() and isset() are both variable processing functions. Their function is to determine whether the variable has been configured. It is precisely because of their great similarity in the process of processing variables that they leading to insufficient understanding of their relationship. If you only consider the two functions empty() and isset() themselves, it will make people more confused. Look at it from another angle. The processing objects of empty() and isset() are nothing more than undefined variables, 0, and empty strings.
If the variable is 0, empty() will return TRUE, isset() will return TRUE;
If the variable is an empty string, empty() will return TRUE, isset() will return TRUE;
If the variable is not defined, Then empty() will return TRUE, isset() will return FLASE;
The explanation of empty() in the manual is as follows:
Description bool empty(mixed var)
If var is a non-empty or non-zero value, 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.
The explanation of isset() in the manual is as follows:
isset() detects whether the variable is set
Description bool isset (mixed var [, mixed var [, ...]] )
Returns TRUE if var exists, otherwise returns FALSE.
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, first use the isset function and then Use empty function

The above introduces the detailed explanation of the difference between PHP isset and empty, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

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.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function