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
文章地址:
转载随意^^请带上本文地址!

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

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

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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