Home php教程 php手册 php判断值是否为空实例代码

php判断值是否为空实例代码

May 25, 2016 pm 04:43 PM
Is it empty

php实际应用中一般都是用isset()函数来做判断的,示例如下:

if( isset($_GET['name']) )//判断传来的值是否为空。   
{   
    echo "值为空";   
}
Copy after login

使用说明:

1、空值、0、false的赋值结果均被isset判为 TRUE

2、NULL将被isset判为 FALSE

3、isset()同样适用于数组元素和对象元素的检查,若数组或对象实例未被定义,则检测其中的数组元素/对象元素将被返回false.

empty功能:检测变量是否为“空”

很简单的,给一个例子,代码如下:

$sql = "select * form abc"  
$result = mysql_query($sql);  
$row = mysql_fetch_array($result);  
if(emptyempty($row))//开始判断是够为空  
{  
    echo "null";  
}  
else  
{  
    echo "not null";  
}
Copy after login

方法二,代码如下:

$sql = "select * form abc"  
$result = mysql_query($sql);  
$row = mysql_fetch_array($result);  
if(!$row)//开始判断是够为空  
{  
    echo "null";  
}  
else  
{  
    echo "not null";  
}
Copy after login

其实mysql_fetch_array函数在使用的时候如果表是空表的话,就会返回false,这个时候$row自然没有赋.

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

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

PHP实例代码如下:

$a = 0;  
$b = array();  
if ($a == null) echo '$a 为空' . "";  
if ($b == null) echo '$b 为空' . "";  
if ($c == null) echo '$b 为空' . "";  
// 显示结果为  
// $a 为空  
// $b 为空  
// Undefined variable: c
Copy after login

4. is_null功能:检测变量是否为“null”

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

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

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

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

PHP实例代码如下:

$a = null;  
$b = false;  
if (is_null($a)) echo '$a 为NULL' . "";  
if (is_null($b)) echo '$b 为NULL' . "";  
if (is_null($c)) echo '$c 为NULL' . "";  
// 显示结果为  
// $a 为NULL  
// Undefined variable: c
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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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)