便利な評価方法 By php

# はじめに
phpを使う上でよくemptyやis_null、issetを使うが、完全に条件を覚えていないので簡単に見返せるよう、自分用に書く。

## empty
emptyは厳しい

|  | bool |
| --- | --- |
| $checkVal=1 | <font color="red">false<font> |
| $checkVal='1' | <font color="red">false<font> |
| $checkVal=0 | <font color="blue">true<font> |
| $checkVal='0' | <font color="blue">true<font> |
| $checkVal=[] | <font color="blue">true<font> |
| $checkVal=[0] | <font color="red">false<font> |
| $checkVal | <font color="blue">true<font> |
| $checkVal='' | <font color="blue">true<font> |
| $checkVal=null | <font color="blue">true<font>


## is_null
is_nullはゆるい

|  | bool |
| --- | --- |
| $checkVal=1 | <font color="red">false<font> |
| $checkVal='1' | <font color="red">false<font> |
| $checkVal=0 | <font color="red">false<font> |
| $checkVal='0' | <font color="red">false<font> |
| $checkVal=[] | <font color="red">false<font> |
| $checkVal=[0] | <font color="red">false<font> |
| $checkVal | <font color="blue">true<font> |
| $checkVal='' | <font color="red">false<font> |
| $checkVal=null | <font color="blue">true<font> |

## if
ifは厳しい

|  | bool |
| --- | --- |
| $checkVal=1 | <font color="blue">true<font> |
| $checkVal='1' | <font color="blue">true<font> |
| $checkVal=0 | <font color="red">false<font> |
| $checkVal='0' | <font color="red">false<font> |
| $checkVal=[] | <font color="red">false<font> |
| $checkVal=[0] | <font color="blue">true<font> |
| $checkVal | <font color="red">false<font> |
| $checkVal='' | <font color="red">false<font> |
| $checkVal=null | <font color="red">false<font> |

## isset
issetはゆるい

|  | bool |
| --- | --- |
| $checkVal=1 | <font color="blue">true<font> |
| $checkVal='1' | <font color="blue">true<font> |
| $checkVal=0 | <font color="blue">true<font> |
| $checkVal='0' | <font color="blue">true<font> |
| $checkVal=[] | <font color="blue">true<font> |
| $checkVal=[0] | <font color="blue">true<font> |
| $checkVal | <font color="red">false<font> |
| $checkVal='' | <font color="blue">true<font> |
| $checkVal=null | <font color="red">false<font> |

## 総評
isがついてたらゆるい

|  | empty | is_null | if | isset |
| --- | --- | --- | --- | --- |
| $checkVal=1 | <font color="red">false<font> | <font color="red">false<font> | <font color="blue">true<font> | <font color="blue">true<font> |
| $checkVal='1' | <font color="red">false<font> | <font color="red">false<font> | <font color="blue">true<font> | <font color="blue">true<font> |
| $checkVal=0 | <font color="blue">true<font> | <font color="red">false<font> | <font color="red">false<font> | <font color="blue">true<font> |
| $checkVal='0' | <font color="blue">true<font> | <font color="red">false<font> | <font color="red">false<font> | <font color="blue">true<font> |
| $checkVal=[] | <font color="blue">true<font> | <font color="red">false<font> | <font color="red">false<font> | <font color="blue">true<font> |
| $checkVal=[0] | <font color="red"><font color="red">false<font><font> | <font color="red">false<font> | <font color="blue">true<font> | <font color="blue">true<font> |
| $checkVal | <font color="blue">true<font> ​| <font color="blue">true<font> | <font color="red">false<font> | <font color="red">false<font> |
| $checkVal='' | <font color="blue">true<font> | <font color="red">false<font> | <font color="red">false<font> | <font color="blue">true<font> |
| $checkVal=null | <font color="blue">true<font> | <font color="blue">true<font> | <font color="red">false<font> | false |