PHP isset() vs empty() vs is_null()

高端一回,转篇英文的吧。

http://techtalk.virendrachandak.com/php-isset-vs-empty-vs-is_null/

PHP has different functions which can be used to test the value of a variable. Three useful functions for this areisset(),empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.

isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.

isset()

From PHP Manual:

isset — Determine if a variable is set and is not NULL

In other words, it returns true only when the variable is not null.

empty()
From PHP Manual:

empty — Determine whether a variable is empty

In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

is_null()
From PHP Manual:

is_null — Finds whether a variable is NULL

In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables.

The table below is an easy reference for what these functions will return for different values. The blank spaces means the function returns bool(false).
Value of variable ($var)isset($var)empty($var)is_null($var)
“” (an empty string)bool(true)bool(true) 
” ” (space)bool(true)  
FALSEbool(true)bool(true) 
TRUEbool(true)  
array() (an empty array)bool(true)bool(true) 
NULL bool(true)bool(true)
“0″ (0 as a string)bool(true)bool(true) 
0 (0 as an integer)bool(true)bool(true) 
0.0 (0 as a float)bool(true)bool(true) 
var $var; (a variable declared, but without a value) bool(true)bool(true)
NULL byte (“\ 0″)bool(true)  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值