PHP’s isset() function is generally used to detect whether a variable is set
Format: bool isset (mixed var [, mixed var [, ...]] )
Function: Check whether the variable is set
Return value:
If the variable does not exist, return FALSE
If the variable exists and its value is NULL, it will also return FALSE
If the variable exists and If the value is not NULL, TRUE will be returned
When checking multiple variables at the same time, TRUE will be returned only when each item meets the previous requirement, otherwise the result will be FALSE
Version: 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 is "", 0, "0", NULL,, FALSE, array( ), var $var; and objects without any attributes, return TRUE
If the variable exists and the value is not "", 0, "0", NULL,, FALSE, array(), var $var; and none If the object has any attribute, it will return FALSE
Versions: PHP 3, PHP 4, PHP 5
More instructions:
The return value of empty() =!(boolean) var, but it will not be because the variable is not Define a warning message. 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 between empty() and isset()
<?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 detailed example code of isset and empty functions that has been tested by Script House. 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>'; } ?>
When using PHP to write page programs, I often use variables The processing function determines whether a variable value at the end of the php page is empty. At first, I was used to using the empty() function, but I found some problems, so I switched to the isset() function and the problem no longer existed.
As the name suggests, empty()
Determine whether a variable is "empty", 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 it will happen
Some surprises. It turns out that empty() and isset()
Although they 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()
This variable is considered to be equal to empty, which is equivalent to no setting.
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 that there is no configuration, isset () can get the value of $id:
$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() and replace it with isset at this time. Be smarter.
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; - If id=0, id will also be 1
if(!isset($id)) $id=1; - If id=0, id will not be 1
You can run the following code separately Check the above inference:
if(empty($id)) $id=1;
print $id; // get 1
if(!isset($id)) $id=1 ;
print $id; //get 0
To talk about their connection, their common points are empty() and
isset() is a variable processing function, and its 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 have insufficient understanding of their relationship. Shan Cong
If the two functions empty() and isset() are considered by themselves, it will make people more confused. Look at them 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, empty() will return TRUE and 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, because 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 determine whether a variable has been declared, you can use the isset function
When you want to determine whether a variable has been assigned data and is not empty, you can use the empty function
When you want to determine a variable If the variable exists and is not empty, first use the isset function and then use the empty function
The above is the detailed content of The difference between isset() and empty() in PHP. For more information, please follow other related articles on the PHP Chinese website!

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor