文章索引:
- isset()、- empty()、- is_null()的区别
- == 和 ===区别
- 类型转换后到底哪些是false?
- 三目运算符区别:? : 、?:和??
- 访问数组中不存在索引时,会怎么样?
isset()、empty()、is_null()的区别
- isset()判断变量是否已设置(set)且非null,isset()官方文档- 变量被unset()后,则isset()返回false
- 变量值为null,则issset()返回false
- isset()函数支持多参数,如- isset($foo, $bar)
- isset()判断数组是否包含特定索引值,注意数组值为null的情形- 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13- $hello = ""; 
 var_dump(isset($hello)); // true
 $world = null;
 var_dump(isset($world)); // false
 var_dump(isset($hello, $world)); // false
 $world = "world";
 var_dump(isset($hello, $world)); // true
 unset($world);
 var_dump(isset($world)); // false
 $hello = ["hello" => "a", "world" => null]; // 注意,值为null时isset()返回false
 var_dump(isset($hello["hello"])); // true
 var_dump(isset($hello["world"])); // false
 
- 变量被
- empty()判断变量值是否为空(且不会产生日E_NOTICE日志事件),empty()官方文档,以下皆为空:- “” (空字符串)
- 0 (作为整数的0)
- 0.0 (作为浮点数的0)
- “0” (作为字符串的0)
- NULL
- FALSE
- array() (一个空数组)
- $var; (一个声明了,但是没有值的变量)
 
- is_null()判断变量是否为null
- null和NULL等价,不区分大小写,官方说明文档
- 变量为null的三种情形:- 变量被赋值为null,如$a = null
- 变量未被定义,如使用未被声明的变量,var_dump($nothing);// null
- 变量已被unset(),如$a = 'a';unset($a);var_dump($a);// $a now is null 
 
- 变量被赋值为null,如
== 和 ===区别
- ==等于(Equal),类型转换后相等,则值为true
- ===恒等(Identical),值和类型均相等,则值为true
 几个例子:- 1 
 2- 123 == "123"; // true 
 123 === "123"; // false,类型不同
这里运用官方的两个图表

类型转换后到底哪些是false?(””、null、false、array()、new class()等)
- 什么值在类型转换后为true/false? 
 下列类型在类型转换后为false,其余均为true:- 布尔型false
- 整型0
- 浮点型0.0
- 空字符串和字符串”0”:特别注意仅包含0的字符串会被转换为false
- 空数组
- null类型变量
- 从空标签创建的SimpleXML对象
 
- 比较运算符是等于(==)还是恒等(===)? 
 二者区别见上文,作为判断条件时注意
三目运算符区别:? : 、?:和??
- 标准三目运算符(Ternary Operator):$res = exp1?exp2:exp3;
- 简化三目运算符:$res = exp1 ?: exp3; // 当exp1==true时取exp1的值,否则为exp3- PHP 5.3后新增语法糖
- exp1变量未定义时会产生E_NOTICE事件
 
- 比较运算符??的叫法是”NULL 合并操作符(Null Coalescing Operator)”- PHP 7中新增语法糖,官方文档
- 表达式 expr1??expr2:当expr1为null时,值为exp2;否则为expr1
- 这种形式在左侧单元为null时,不会产生notice事件,和isset()一样,所以在判断数组索引键时比较实用$a ?? $b ?? $c:从左往右第一个存在且不为 NULL 的操作数。如果都没有定义且不为 NULL,则返回 NULL。PHP7开始提供。 
 
实例代码:1
2
3
4
5
6
7$a = “default”;
$res = $a ? “success” : "fail"; // success
$res = $a ?: "fail"; // default
$res = $a??"nothing"; // default
$a = ['a' => 'aaa', 'b'=> 'bbb'];
$res = $a['hello'] ?? 'default'; // default,且没有notice事件日志
访问数组不存在的索引时,会怎么样?
- 通过isset()来判断数据中是否存在特定索引
- 直接访问数据中不存在的索引,默认返回值为null,但会有Notice日志 - 1 
 2
 3- [25-Nov-2017 18:59:28 Asia/Shanghai] PHP Notice: Undefined index: not_exist in /home/work/www/xxxxx/public/index.php on line 4 
 [25-Nov-2017 18:59:28 Asia/Shanghai] PHP Stack trace:
 [25-Nov-2017 18:59:28 Asia/Shanghai] PHP 1. {main}() /home/work/www/xxxxx/public/index.php:0
- 注意:Laravel中如果直接数组不存在索引会抛出ErrorException(Laravel 5.x中默认错误级别是 - error_reporting(-1)),从而导致接口请求500,框架日志中错误信息如下:- 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16- [2017-11-25 18:57:59] lumen.ERROR: ErrorException: Undefined index: cccc in /home/work/www/xxxxx/app/Http/Controllers/xxxxxController.php: 
 69 Stack trace:
 #0 /home/work/www/xxxxx/app/Http/Controllers/xxxxxController.php(69):
 Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(8, 'Undefined index...', '/home/work/www/...', 69, Array) #1 [internal function]:
 App\Http\Controllers\xxxxxController->xxxxx(Object(Illuminate\Http\Request)) #2 /home/work/www/xxxxx/vendor/illuminate/container/Container.php(507):
 call_user_func_array(Array, Array) #3 /home/work/www/xxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(586):
 Illuminate\Container\Container->call(Array, Array) #4 /home/work/www/xxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(552):
 Laravel\Lumen\Application->callControllerCallable(Array, Array) #5 /home/work/www/xxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(526):
 Laravel\Lumen\Application->callLumenController(Object(App\Http\Controllers\xxxxxController), 'xxxxx', Array) #6 /home/work/www/xxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(494):
 Laravel\Lumen\Application->callControllerAction(Array) #7 /home/work/www/xxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(479):
 Laravel\Lumen\Application->callActionOnArrayBasedRoute(Array) #8 /home/work/www/xxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(376):
 Laravel\Lumen\Application->handleFoundRoute(Array) #9 /home/work/www/xxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(629):
 Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}() #10 /home/work/www/xxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(382):
 Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure)) #11 /home/work/www/xxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(327):
 Laravel\Lumen\Application->dispatch(NULL) #12 /home/work/www/xxxxx/public/index.php(34):
 Laravel\Lumen\Application->run() #13 {main} [] []
- 可在 - app/Providers/AppServiceProvider.php的- register()函数中增加- error_reporting(E_ALL ^ E_NOTICE);来自定义错误级别
- 也可以在程序逻辑中增加数组索引的isset()或empty()判断1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11// Laravel中正确姿势,进行代码防御 
 $hello = ["a" => "aaaa"];
 if (!isset($hello["not_exist"])) {
 return "Undefined index in hello";
 }
 // continue other thing
 // PHP 7可以使用??运算符进行处理
 // 数组中不存在则取空字符串,由于??运算符不产生notice,所以不会导致Laravel触发ErrorException
 $name = $hello["name"] ?? 'default'; // 注意不能用简化的三目运算符?:
 $name = empty($hello['name'])?$hello['name']:'default';
结论:不产生日志事件的三个操作,其余情况会产生E_NOTICE事件(Undefined variable或Undefined index)
- isset($hello['nothig'])
- empty($hello['nothig'])
- $hello['nothig']??'default'