search
HomeBackend DevelopmentPHP Tutorialphp如何判断变量是否为空?

php如何判断变量是否为空?

Jun 13, 2016 am 11:23 AM
php

php如何判断变量是否为空?

php判断变量是否为空的几种方法

1、使用isset()

说明:它会判断变量是否为空,并且可以用来判断数组中元素是否被定义过

注意:当使用isset来判断数组元素是否被初始化过时,它的效率比array_key_exists高4倍左右

<?php
$a = &#39;&#39;;
$a[&#39;c&#39;] = &#39;&#39;;
if (!isset($a)) echo &#39;$a 未被初始化&#39; . "";
if (!isset($b)) echo &#39;$b 未被初始化&#39; . "";
if (isset($a[&#39;c&#39;])) echo &#39;$a 已经被初始化&#39; . "";
// 显示结果为
// $b 未被初始化
// $a 已经被初始化

2、使用empty

说明:任何一个未初始化的变量、值为 0 或 false 或 空字符串”” 或 null的变量、空数组、没有任何属性的对象,都将判断为empty==true

注意1:未初始化的变量也能被empty检测为”空”

注意2:empty只能检测变量,而不能检测语句

<?php
header("Content-Type: text/html; charset=utf-8");
$a = 0;
$b = &#39;&#39;;
$c = array();
if (empty($a)) echo &#39;$a 为空&#39; . "<br>";
if (empty($b)) echo &#39;$b 为空&#39; . "<br>";
if (empty($c)) echo &#39;$c 为空&#39; . "<br>";
?>

输出:

$a 为空
$b 为空
$c 为空

3、使用==判断变量是否等于null,判断变量是否为”空”

说明:值为 0 或 false 或 空字符串”” 或 null的变量、空数组、都将判断为 null

注意:与empty的显著不同就是:变量未初始化时 var == null 将会报错。

<?php
$a = 0;
$b = array();
if ($a == null) echo &#39;$a 为空&#39; . "";
if ($b == null) echo &#39;$b 为空&#39; . "";
if ($c == null) echo &#39;$b 为空&#39; . "";
// 显示结果为
// $a 为空
// $b 为空
// Undefined variable: c

4、使用 is_null,检测变量是否为”null”

说明:当变量被赋值为”null”时,检测结果为true

注意1:null不区分大小写:$a = null; $a = NULL 没有任何区别

注意2:仅在变量的值为”null”时,检测结果才为true ,      0、空字符串、false、空数组都检测为false

注意3:变量未初始化时,程序将会报错

$a = null;
$b = false;
if (is_null($a)) echo &#39;$a 为NULL&#39; . "";
if (is_null($b)) echo &#39;$b 为NULL&#39; . "";
if (is_null($c)) echo &#39;$c 为NULL&#39; . "";
// 显示结果为
// $a 为NULL
// Undefined variable: c

5、使用===判断变量是否等于null

检测变量是否为”null”,同时变量的类型也必须是”null”

说明:当变量被赋值为”null”时,同时变量的类型也是”null”时,检测结果为true

注意1:在判断为”null”上,全等于和is_null的作用相同

注意2:变量未初始化时,程序将会报错

总结:

PHP中,”NULL” 和 “空” 是2个概念。

isset 主要用来判断变量是否被初始化过

empty 可以将值为 “假”、”空”、”0″、”NULL”、”未初始化” 的变量都判断为TRUE

is_null 仅把值为 “NULL” 的变量判断为TRUE

var == null 把值为 “假”、”空”、”0″、”NULL” 的变量都判断为TRUE

var === null 仅把值为 “NULL” 的变量判断为TRUE

注意:在判断一个变量是否真正为”NULL”时,大多使用 is_null,从而避免”false”、”0″等值的干扰。

更多相关知识,请访问 PHP中文网!!

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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 Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

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

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

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.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor