


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>'; } ?>When using PHP to write page programs, I often use variable processing functions to determine certain parameters at the end of the PHP page. Whether the value of each 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 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(). 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; //得到 0To say Their connection has in common 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 are Insufficient understanding of relationships. 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 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

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
