Home Backend Development PHP Tutorial PHP isset()与empty()的施用区别详解

PHP isset()与empty()的施用区别详解

Jun 13, 2016 pm 01:18 PM
br echo gt lt var

PHP isset()与empty()的使用区别详解

引用
通过对PHP语言的学习,应该知道它是基于函数的一款HTML脚本语言。庞大的函数库支持着PHP语言功能的实现。下面我们为大家介绍有关PHP函数isset()与empty()的相关用法。
PHP的isset()函数 一般用来检测变量是否设置
格式:bool isset ( mixed var [, mixed var [, ...]] )
功能:检测变量是否设置
返回值:
若变量不存在则返回 FALSE
若变量存在且其值为NULL,也返回 FALSE
若变量存在且值不为NULL,则返回 TURE
同时检查多个变量时,每个单项都符合上一条要求时才返回 TRUE,否则结果为 FALSE
版本:PHP 3, PHP 4, PHP 5
更多说明:
使用 unset() 释放变量之后,它将不再是 isset()。
PHP函数isset()只能用于变量,传递任何其它参数都将造成解析错误。
检测常量是否已设置可使用 defined() 函数。


PHP的empty()函数 判断值为否为空
格式:bool empty ( mixed var )
功能:检查一个变量是否为空
返回值:
若变量不存在则返回 TRUE
若变量存在且其值为""、0、"0"、NULL、、FALSE、array()、var $var; 以及没有任何属性的对象,则返回 TURE
若变量存在且值不为""、0、"0"、NULL、、FALSE、array()、var $var; 以及没有任何属性的对象,则返回 FALSE
版本:PHP 3, PHP 4, PHP 5
更多说明:
empty()的返回值=!(boolean) var,但不会因为变量未定义而产生警告信息。参见转换为布尔值获取更多信息。
empty() 只能用于变量,传递任何其它参数都将造成Paser error而终止运行。
检测常量是否已设置可使用 defined() 函数。

例子: empty() 与 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'; 
} 
?> 

注: 由于这是一个语言结构而非函数,因此它无法被变量函数调用。 
注: empty() 只检测变量,检测任何非变量的东西都将导致解析错误。换句话说,后边的语句将不会起作用: empty(addslashes($name))。 
Copy after login

下面是经过脚本之家测试过的一段isset与empty函数详细例子的代码,看完这个基本上就差不多了:
复制代码 代码如下:
<?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>'; 
} 
?>
Copy after login
Statement of this Website
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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

&quot;Go Language Development Essentials: 5 Popular Framework Recommendations&quot; As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

Implementing distributed task scheduling using Golang's web framework Echo framework Implementing distributed task scheduling using Golang's web framework Echo framework Jun 24, 2023 am 11:49 AM

With the development of the Internet and the advancement of information technology, the era of big data has arrived, and fields such as data analysis and machine learning have also been widely used. In these fields, task scheduling is an inevitable problem. How to achieve efficient task scheduling is crucial to improving efficiency. In this article, we will introduce how to use Golang's web framework Echo framework to implement distributed task scheduling. 1. Introduction to the Echo framework Echo is a high-performance, scalable, lightweight GoWeb framework. It is based on HTTP

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

See all articles