empty与isset,null与undefined
一. null VS undefined VS NaN
1. null
定义:null是特殊的object,是空对象,没有任何属性和方法。
document.writeln(typeof null); // object
值得注意的是: 对象模型中,所有的对象都是Object或其子类的实例,但null对象例外:
document.writeln(null instanceof Object); // false
2. undefined
定义:undefined是undefined类型的值,未定义的值和定义未赋值的为undefined;无法使用 for/in 循环来枚举 undefined 属性,也不能用 delete 运算符来删除它。
作用:
(1)变量被声明了,但没有赋值时,就等于undefined。
(2) 调用函数时,应该提供的参数没有提供,该参数等于undefined。
(3)对象没有赋值的属性,该属性的值为undefined。
(4)函数没有返回值时,默认返回undefined。
var i;
i // undefined function f(x){console.log(x)}
f() // undefined var o = new Object();
o.p // undefined var x = f();
x // undefined
提示:只能用 === 运算来测试某个值是否是未定义的,因为 == 运算符认为 undefined 值等价于 null。
注释:null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。
3. NaN
NaN是一种特殊的number
document.writeln(typeof NaN); // number
他们三个的值
1. null“等值(==)”于undefined,但不“全等值(===)”于undefined,NaN与任何值都不相等,与自己也不相等:
document.writeln(null == undefined); // true
document.writeln(null === undefined); // false
document.writeln(NaN == null); // false
document.writeln(NaN == undefined); // false
document.writeln(NaN == NaN); // false
2. null与undefined都可以转换成false,但不等值于false:
document.writeln(!null, !undefined); // true true
document.writeln(undefined == false); // false
document.writeln(null == false); // false
3. null不等于0,但是计算中null会当做成0来处理;undefined计算的结果是NaN;NaN计算的结果也是NaN:
alert(null == 0); // false
alert(123 + null); // 123
alert(123 * null); // 0
alert(123 / null); // Infinity
alert(123 + undefined); // NaN
alert(123 * undefined); // NaN
alert(123 / undefined); // NaN
alert(123 + NaN); // NaN
alert(123 * NaN); // NaN
alert(123 / NaN); // NaN
二. empty VS isset
1. empty
检查一个变量是否为空(变量不存在或它的值等同于FALSE),如果变量不存在empty()不会产生警告。

2. isset
检测变量是否设置,并且不是 NULL。
<?php $var = ''; // 结果为 TRUE,所以后边的文本将被打印出来。
if (isset($var)) {
echo "This var is set so I will print.";
} // 在后边的例子中,我们将使用 var_dump 输出 isset() 的返回值。
// the return value of isset(). $a = "test";
$b = "anothertest"; var_dump(isset($a)); // TRUE
var_dump(isset($a, $b)); // TRUE unset ($a); var_dump(isset($a)); // FALSE
var_dump(isset($a, $b)); // FALSE $foo = NULL;
var_dump(isset($foo)); // FALSE ?>
empty与isset,null与undefined的更多相关文章
- null、 is_null() 、empty() 、isset() PHP 判断变量是否为空
PHP中,在判断变量是否为空的时候,总会纠结应该选用哪个函数,下面列取常用的多种情况,其中1/3经过我的验证,其它来自网络,验证后使用... 使用 PHP 函数对变量 $x 进行比较 表达式 gett ...
- empty、isset、is_null的比较
直接上代码 <?php $a=0; $b='0'; $c=0.0; $d=''; $e=NULL; $f=array(); $g='\0'; $h=' ';//space $i=true; $j ...
- empty、isset、is
直接上代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <?php $a=0; $b='0'; $c=0.0; ...
- [PHP源码阅读]empty和isset函数
近日被问到PHP中empty和isset函数时怎么判断变量的,刚开始我是一脸懵逼的,因为我自己也只是一知半解,为了弄懂其真正的原理,赶紧翻开源码研究研究.经过分析可发现两个函数调用的都是同一个函数,因 ...
- empty和isset函数详解
1.empty函数 用途:检测变量是否为空 若变量不存在则返回 TRUE 若变量存在且其值为"".0."0".NULL..FALSE.array().var $ ...
- php empty()和isset()的区别
在使用 php 编写页面程序时,我经常使用变量处理函数判断 php 页面尾部参数的某个变量值是否为空,开始的时候我习惯了使用 empty() 函数,却发现了一些问题,因此改用 isset() 函数,问 ...
- php部分---include()与require()的区别、empty()与isset is_null的区别与用法详解
include()与require()的用途是完全一样的,不一定非得哪个放在最前面哪个放在中间.他们最根本的区别在于错误处理的方式不一样. 1.处理错误的方式: require()一个文件存在错误的话 ...
- php中empty(), is_null(), isset()函数区别
empty(), is_null(), isset()真值表(区别) 我们先来看看这3个函数的功能描述 www.111cn.net isset 判断变量是否已存在,如果变量存在则返回 TRUE,否则返 ...
- php中empty()、isset()、is_null()和变量本身的布尔判断区别(转)
在php脚本中,我们经常要去判断一个变量是否已定义或者是否为空,就需要用到这些函数empty().isset().is_null()和其本身作为参数,下面小段程序做个简要比较 <?php//预定 ...
随机推荐
- HTTPS 常见部署问题及解决方案
在最近几年里,我写了很多有关 HTTPS 和 HTTP/2 的文章,涵盖了证书申请.Nginx 编译及配置.性能优化等方方面面.在这些文章的评论中,不少读者提出了各种各样的问题,我的邮箱也经常收到类似 ...
- 基于token的后台身份验证(转载)
几种常用的认证机制 HTTP Basic Auth HTTP Basic Auth简单点说明就是每次请求API时都提供用户的username和password,简言之,Basic Auth是配合RES ...
- jQuery基础(鼠标事件,表单事件,键盘事件,自定义事件 篇)
1.jQuery鼠标事件之click与dbclick事件 方法一:$ele.click()(不带参数) <div id="test">点击触发<div&g ...
- linux 软件包 rpm命令之安装、更新、卸载、依赖
软件包分类1.源码包2.二进制包二进制包是源码包编译后产生的文件..exe文件是适用于windows平台的二进制包:RPM包适用于redhat系列的二进制包:deb包是适用于ubuntu平台的二进制包 ...
- lombok的简单介绍(2)
在和idea中整合遇到这样问题,实体对象不提示lombok的get/set方法,从网上找到以下方法,分享给大家
- leetcode题解之Find the Duplicate Number
1.题目描述 2.分析 利用C++的 标准模板库 set 对数组进行读取,然后插入,如果检测到元素已经在set内部,则返回该元素值即可.时间复杂度为 O(n),空间复杂度为 O(n); 3.代码 in ...
- leetcode 之 Degree of an Array
1.题目描述 Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...
- iOS7中修改StatusBar的显示颜色
iOS7中修改StatusBar的显示颜色 效果图如下: 在iOS7中想手动修改statusBar的颜色,第一步需要做的就是在plist文件中设置View controller-based statu ...
- swift关于UIView设置frame值的extension
swift关于UIView设置frame值的extension 使用 说明 1. 使用如上图,很简单,不再赘述 2. 在extension给添加的计算属性提供getter,setter方法即可 源码 ...
- [C++] 用Xcode来写C++程序[7] Class
用Xcode来写C++程序[7] Class 不带构造函数的Rectangle类 // // Rectangle.h // Plus // // Created by YouXianMing on 1 ...