Chinaunix首页 | 论坛 | 博客
  • 博客访问: 572316
  • 博文数量: 84
  • 博客积分: 1529
  • 博客等级: 上尉
  • 技术积分: 1482
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 17:57
文章分类

全部博文(84)

文章存档

2014年(7)

2013年(9)

2012年(20)

2011年(48)

分类: PHP

2013-10-28 22:41:19

最近写了一句判断的程序

点击(此处)折叠或打开

  1. $switchType= empty($switchType)? 'all' : $switchType;


也就是在$switchType为空的情况下赋值为all,发现当$switchType为0的时候会发现$switchType也被赋值为all,因此可以认定php中0这个值在empty函数中会被作为空来判定,后来该为用isset就解决了此问题。根据这个问题,通过实际例子详细来解释下empty和issset两者的区域和用法。

一、empty和isset函数详解
1、empty函数 
用途:检测变量是否为空
判断:如果 var 是非空或非零的值,则 empty() 返回 FALSE。换句话说,""、0、"0"、NULL、FALSE、array()、var $var; 以及没有任何属性的对象都将被认为是空的,如果 var 为空,则返回 TRUE。来源手册:2、isset函数
用途:检测变量是否设置
判断:检测变量是否设置,并且不是 NULL。如果已经使用  释放了一个变量之后,它将不再是 isset()。若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE。同时要注意的是一个NULL 字节("\0")并不等同于 PHP 的 NULL 常数。

二、测试例子

点击(此处)折叠或打开

  1. <?php
  2. function test($test_value) {
  3.         var_dump($test_value);
  4.         if (empty($test_value)) {
  5.                 echo $test_value . " empty\n";
  6.         }
  7.         else {
  8.                 echo $test_value . " not empty\n";
  9.         }

  10.         if (isset($test_value)) {
  11.                 echo $test_value . " isset\n";
  12.         }
  13.         else {
  14.                 echo $test_value . " not isset\n";
  15.         }

  16.         if ($test_value == "") {
  17.             echo $test_value . " the string empty\n";
  18.         }
  19.         else {
  20.             echo $test_value . " the string not empty\n";
  21.         }
  22. }

  23. $test_value = 0;
  24. test($test_value);
  25. echo "-----------------------\n";
  26. $test_value = NULL;
  27. test($test_value);
  28. echo "-----------------------\n";

  29. $test_value = "";
  30. test($test_value);
  31. echo "-----------------------\n";

  32. $test_value = "\0";
  33. test($test_value);
  34. echo "-----------------------\n";

  35. $test_value = array();
  36. test($test_value);
  37. echo "-----------------------\n";

  38. $test_value = false;
  39. test($test_value);
  40. echo "-----------------------\n";

  41. $test_value = true;
  42. test($test_value);
  43. echo "-----------------------\n";
  44. ?>
结果:

点击(此处)折叠或打开

  1. int(0)
  2. 0 empty
  3. 0 isset
  4. 0 the string empty
  5. -----------------------
  6. NULL
  7.  empty
  8.  not isset
  9.  the string empty
  10. -----------------------
  11. string(0) ""
  12.  empty
  13.  isset
  14.  the string empty
  15. -----------------------
  16. string(1) ""
  17.  not empty
  18.  isset
  19.  the string not empty
  20. -----------------------
  21. array(0) {
  22. }
  23. Array empty
  24. Array isset
  25. Array the string not empty
  26. -----------------------
  27. bool(false)
  28.  empty
  29.  isset
  30.  the string empty
  31. -----------------------
  32. bool(true)
  33. 1 not empty
  34. 1 isset
  35. 1 the string not empty
  36. -----------------------


三、两者区别
1、isset值对于变量没有赋值或者赋值为NULL时判断false,其余都是true;
2、empty需要注意点比较多,要根据函数的定义来做判断
四、使用注意
empty在用于判断比对的时候要多注意,0若是用==会和""相等,此时可能需要用强制等于来判断===

阅读(5557) | 评论(0) | 转发(0) |
0

上一篇:json和xml

下一篇:tcpdump和tcpflow命令实践

给主人留下些什么吧!~~