PHP isset() vs empty() vs is_null()
2016-03-30 10:26
579 查看
PHP isset() vs empty() vs is_null()
ByVirendra Chandak onJanuary 21, 2012
Leave a commentGo
to comments (50)
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()
FromPHP manual – isset():
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()
FromPHP Manual – empty():
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()
FromPHP Manual – is_null():
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 returnsbool(false).
Value of variable ($var) | isset($var) | empty($var) | is_null($var) |
---|---|---|---|
“” (an empty string) | bool(true) | bool(true) | |
” ” (space) | bool(true) | ||
FALSE | bool(true) | bool(true) | |
TRUE | bool(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) |
PHP 7.0.4
PHP 5.6.19
PHP 5.5.33
View Demo
Download Source Code
Share this:
TweetRelated Posts
Get search query string from search engines using PHPHow to apply a function to every array element in PHP
PHP 7 – Null Coalesce Operator
Getting real client IP address in PHP
PHP,Web
Development
empty,isset,
is_null,
PHP
← Make a div stick to top when scrolled to
Get search query string from search engines using PHP→
Leave a comment ?
相关文章推荐
- windows下PHP拓展包的选择
- YII Framework的filter过滤器用法分析
- 为PHP添加GD库支持
- yii的验证码
- 文件传输(FTP)服务器
- OLTP与OLAP的区别
- 小數點的運算[討論區- PHP新手區] : 台灣PHP聯盟
- php学习 SQL注入 2016.03.29回顾
- cmd 运行php文件详解
- php启动时候提示PHP startup的解决方法
- phplist及phpmailer(组合使用)通过gmail发送邮件的配置方法
- HBase启动后RegionServer自动挂原因及解决办法【ntp】
- PHP的邮件群发系统phplist配置方法详细总结
- Internal compiler error. See the console log for more information. output was:
- Laravel路由设定和子路由设定实例分析
- RFC3984: RTP Payload Format for H.264 Video(中文版)
- laravel_5《数据库迁移》
- Web Service实例
- 使用ViewPager设置适配器FragmentPagerAdapter出现页面空白的问题
- 一看就懂系列之 php设计模式(一)