empty是判断变量值是非空或非零的值。对应空定义包括:“”(空字符串)、0、“0”、NULL、FALSE、array()和$var(只声明但未赋值)。也就是说当变量值为上述这些,empty返回TRUE,其他的都返回FALSE。 isset是检测变量是否设置,并且不是 NULL。变量设置可以从
empty是判断变量值是非空或非零的值。对应空定义包括:“”(空字符串)、0、“0”、NULL、FALSE、array()和$var(只声明但未赋值)。也就是说当变量值为上述这些,empty返回TRUE,其他的都返回FALSE。
isset是检测变量是否设置,并且不是 NULL。变量设置可以从几个方面来说。1:最简单的就是变量是否先声明和赋值;2:array中是否存在对应的index或key;3:object中是否存在对应的属性。
从上面的两个function定义可以看到,在某些情况下,两者可以公用,但其区别还是很大的。另外它们都只能检测变量,检测任何非变量的东西都将导致解析错误。例如直接检查另一个function的返回值(empty(otherFunction())),你将看到“Fatal error: Can’t use function return value in write context in”这样的错误。
另外isset可以一次检查多个变量,例如:isset($var1, $var2, $var3),当这三个值分别的isset都为TRUE结果为TRUE,否则结果为FALSE。
测试代码:
$sep = ""; echo 'test undeclared var empty : '; var_dump(empty($var)); // TRUE echo $sep . 'test undeclared var isset : '; var_dump(isset($var)); // FALSE $var; echo $sep . 'test declared var but no set value empty : '; var_dump(empty($var)); // TRUE echo $sep . 'test declared var but no set value isset : '; var_dump(isset($var)); // FALSE, 变量申明未赋值,默认值为NULL $var = NULL; echo $sep . 'test declared var and set value NULL empty : '; var_dump(empty($var)); // TRUE echo $sep . 'test declared var and set value NULL isset : '; var_dump(isset($var)); // FALSE, 变量申明赋值为NULL $var1 = ''; $var2 = '0'; $var3 = 0; $var4 = FALSE; $var5 = array(); echo $sep . 'test \'\' empty : '; var_dump(empty($var1)); // TRUE echo $sep . 'test \'0\' empty : '; var_dump(empty($var2)); // TRUE echo $sep . 'test 0 empty : '; var_dump(empty($var3)); // TRUE echo $sep . 'test FALSE empty : '; var_dump(empty($var4)); // TRUE echo $sep . 'test array() empty : '; var_dump(empty($var5)); // TRUE echo $sep . 'test \'\', \'0\', 0, FALSE, array() isset : '; var_dump(isset($var1, $var2, $var3, $var4, $var5)); // TRUE, 变量申明并赋值为空字符串
输出的结果为:
test undeclared var empty : bool(true) test undeclared var isset : bool(false) test declared var but no set value empty : bool(true) test declared var but no set value isset : bool(false) test declared var and set value NULL empty : bool(true) test declared var and set value NULL isset : bool(false) test '' empty : bool(true) test '0' empty : bool(true) test 0 empty : bool(true) test FALSE empty : bool(true) test array() empty : bool(true) test '', '0', 0, FALSE, array() isset : bool(true)
原文地址:也谈empty与isset区别, 感谢原作者分享。

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

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

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.

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.

Atom editor mac version download
The most popular open source editor

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),