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

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

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

使用说明:

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";  
}

方法二,代码如下:

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

其实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

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

               
               

文章地址:

转载随意^^请带上本文地址!

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

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

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