PHP踩坑注意点,你有被坑到过吗?

isset函数:当变量值为null,isset检测变量返回false。

$a = null;
var_dump(isset($a));   //false
$a = '';
var_dump(isset($a));   //true
$a = false;
var_dump(isset($a));   //true
$a = 0;
var_dump(isset($a));   //true

exit()和die()在参数为数字的时候是不会有输出的

exit(999); //空白
//或
die(123); //空白

count函数使用细节注意点:

var_export([
  'count([132, 21, 3836, 7874])' => count([132, 21, 3836, 7874]), //4  数组
  'count(["xx", "yy", "zz"])' => count(["xx", "yy", "zz"]), //3  数组
  
  //非数组,一般都是1
  'count(false)' => count(false), //1
  'count(true)' => count(true), //1
  'count(0)' => count(0), //1
  'count(1)' => count(1), //1
  'count(3)' => count(3), //1
  'count("abc")' => count("abc"), //1
  'count($obj)' => count(new stdClass()), //1  对象哦
  
  //null的话就是0
  'count(null)' => count(null), //0
  "count('')" => count(''), //0
  'count([])' => count([]), //0
]);

运行结果:

count

字符串与数字0比较的注意点:

var_export([
  'a == 0' => 'a' == 0, //true
  'abcd == 0' => 'abcd' == 0, //true
  '中文 == 0' => '中文' == 0, //true
  'a1 == 0' => 'a1' == 0, //true
  'a123456 == 0' => 'a123456' == 0, //true
  
  '0a == 0' => '0a' == 0, //true
  '012a == 0' => '012a' == 0, //false
]);

运行结果:compare

字母开头的字符串 与 数字0 用 == 模糊比较总是true

解决办法:

  • 当然是将 == 比较符写成 === 啦
  • 将请求参数类型强制转换