Backend Development
PHP Tutorial
What is the difference between the use of isset function and empty function in php? (comprehensive)What is the difference between the use of isset function and empty function in php? (comprehensive)
What this article brings to you is about the difference between the use of isset function and empty function in PHP? (Comprehensive), it has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
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, also returns FALSE
If the variable exists and the value is not NULL, TRUE is returned
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 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 objects without any attributes, return FALSE
Versions: PHP 3, PHP 4, PHP 5
More instructions:
The return value of empty() = !(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()
<?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 Called by variable function.
Note: empty() only detects variables, and 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 writing page programs in PHP, I often use variable processing functions to determine 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 isset. () function, the problem is no longer there.
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( ) 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, use Be careful with empty(), it would be wiser to replace it with isset in this case.
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
The following code can be run separately to detect the above inference:
if(empty($id)) $id=1; print $id; // 得到 1 if(!isset($id)) $id=1; print $id; //得到 0
To talk about their connection, their common point is that empty() and isset() are both variable processing functions, and their function is to determine whether the variable is Already configured, it is precisely because of their great similarity in the process of processing variables that they have insufficient understanding of their relationship. Just considering the two functions empty() and isset() themselves 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 and isset() will return TRUE;
If the variable is an empty string, empty() will return TRUE, isset() will return TRUE;
If the variable is undefined, 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, 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.
You can use the isset function when you want to determine whether a variable has been declared.
You can use it when you want to determine whether a variable has been assigned data and is not empty. empty function
When you want to determine that a variable exists and is not empty, first use the isset function and then use the empty function
Related recommendations:
The empty() function and isset(() in PHP ) Function
PHP The difference between isset() function and empty() function_PHP tutorial
The above is the detailed content of What is the difference between the use of isset function and empty function in php? (comprehensive). For more information, please follow other related articles on the PHP Chinese website!
PHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AMPHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.
PHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AMPHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.
Why Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AMThe core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.
Debunking the Myths: Is PHP Really a Dead Language?Apr 16, 2025 am 12:15 AMPHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.
The PHP vs. Python Debate: Which is Better?Apr 16, 2025 am 12:03 AMPHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.
PHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AMPHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.
PHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AMPHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AMUsing preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


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

Dreamweaver Mac version
Visual web development tools

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),





