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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
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

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

What are the most popular golang frameworks on the market? What are the most popular golang frameworks on the market? Jun 01, 2024 pm 08:05 PM

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.

Detailed explanation of the role and usage of the echo keyword in PHP Detailed explanation of the role and usage of the echo keyword in PHP Jun 28, 2023 pm 08:12 PM

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

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

18 Ways to Fix Audio Service Not Responding Issue on Windows 11 18 Ways to Fix Audio Service Not Responding Issue on Windows 11 Jun 05, 2023 pm 10:23 PM

Audio output and input require specific drivers and services to work as expected on Windows 11. These sometimes end up running into errors in the background, causing audio issues like no audio output, missing audio devices, distorted audio, etc. How to Fix Audio Service Not Responding on Windows 11 We recommend you to start with the fixes mentioned below and work your way through the list until you manage to resolve your issue. The audio service may become unresponsive for a number of reasons on Windows 11. This list will help you verify and fix most issues that prevent audio services from responding on Windows 11. Please follow the relevant sections below to help you through the process. Method 1: Restart the audio service. You may encounter

See all articles