てっくめも

主に技術的なことをつらつらと

PHPの判定あれこれ

PHP入門者にissetの判定などを説明する時に、口ではとても説明できなかったのでまとめてみた。

検証環境

サンプルプログラム

<?php

$null = null;
$empty_string = "";
$string = "a";
$integer = 1;
$numeric = "1";
$zero_integer = 0;
$zero_numeric = "0";
$negative_integer = -1;
$negative_numeric = "-1";
$array = array(1);
$array_empty = array();
$true = true;
$false = false;

echo "# DIRECTn";
echo '$null = ' . ($null ? 'TRUE' : 'FALSE') . "n";
echo '$empty_string = ' . ($empty_string ? 'TRUE' : 'FALSE') . "n";
echo '$string = ' . ($string ? 'TRUE' : 'FALSE') . "n";
echo '$integer = ' . ($integer ? 'TRUE' : 'FALSE') . "n";
echo '$numeric = ' . ($numeric ? 'TRUE' : 'FALSE') . "n";
echo '$zero_integer = ' . ($zero_integer ? 'TRUE' : 'FALSE') . "n";
echo '$zero_numeric = ' . ($zero_numeric ? 'TRUE' : 'FALSE') . "n";
echo '$negative_integer = ' . ($negative_integer ? 'TRUE' : 'FALSE') . "n";
echo '$negative_numeric = ' . ($negative_numeric ? 'TRUE' : 'FALSE') . "n";
echo '$array = ' . ($array ? 'TRUE' : 'FALSE') . "n";
echo '$array_empty = ' . ($array_empty ? 'TRUE' : 'FALSE') . "n";
echo '$true = ' . ($true ? 'TRUE' : 'FALSE') . "n";
echo '$false = ' . ($false ? 'TRUE' : 'FALSE') . "n";

echo "----------n";

echo "# issetn";
echo '$null = ' . (isset($null) ? 'TRUE' : 'FALSE') . "n";
echo '$empty_string = ' . (isset($empty_string) ? 'TRUE' : 'FALSE') . "n";
echo '$string = ' . (isset($string) ? 'TRUE' : 'FALSE') . "n";
echo '$integer = ' . (isset($integer) ? 'TRUE' : 'FALSE') . "n";
echo '$numeric = ' . (isset($numeric) ? 'TRUE' : 'FALSE') . "n";
echo '$zero_integer = ' . (isset($zero_integer) ? 'TRUE' : 'FALSE') . "n";
echo '$zero_numeric = ' . (isset($zero_numeric) ? 'TRUE' : 'FALSE') . "n";
echo '$negative_integer = ' . (isset($negative_integer) ? 'TRUE' : 'FALSE') . "n";
echo '$negative_numeric = ' . (isset($negative_numeric) ? 'TRUE' : 'FALSE') . "n";
echo '$array = ' . (isset($array) ? 'TRUE' : 'FALSE') . "n";
echo '$array_empty = ' . (isset($array_empty) ? 'TRUE' : 'FALSE') . "n";
echo '$true = ' . (isset($true) ? 'TRUE' : 'FALSE') . "n";
echo '$false = ' . (isset($false) ? 'TRUE' : 'FALSE') . "n";

echo "----------n";

echo "# emptyn";
echo '$null = ' . (empty($null) ? 'TRUE' : 'FALSE') . "n";
echo '$empty_string = ' . (empty($empty_string) ? 'TRUE' : 'FALSE') . "n";
echo '$string = ' . (empty($string) ? 'TRUE' : 'FALSE') . "n";
echo '$integer = ' . (empty($integer) ? 'TRUE' : 'FALSE') . "n";
echo '$numeric = ' . (empty($numeric) ? 'TRUE' : 'FALSE') . "n";
echo '$zero_integer = ' . (empty($zero_integer) ? 'TRUE' : 'FALSE') . "n";
echo '$zero_numeric = ' . (empty($zero_numeric) ? 'TRUE' : 'FALSE') . "n";
echo '$negative_integer = ' . (empty($negative_integer) ? 'TRUE' : 'FALSE') . "n";
echo '$negative_numeric = ' . (empty($negative_numeric) ? 'TRUE' : 'FALSE') . "n";
echo '$array = ' . (empty($array) ? 'TRUE' : 'FALSE') . "n";
echo '$array_empty = ' . (empty($array_empty) ? 'TRUE' : 'FALSE') . "n";
echo '$true = ' . (empty($true) ? 'TRUE' : 'FALSE') . "n";
echo '$false = ' . (empty($false) ? 'TRUE' : 'FALSE') . "n";

echo "----------n";

echo "# == nulln";
echo '$null = ' . (($null == null) ? 'TRUE' : 'FALSE') . "n";
echo '$empty_string = ' . (($empty_string == null) ? 'TRUE' : 'FALSE') . "n";
echo '$string = ' . (($string == null) ? 'TRUE' : 'FALSE') . "n";
echo '$integer = ' . (($integer == null) ? 'TRUE' : 'FALSE') . "n";
echo '$numeric = ' . (($numeric == null) ? 'TRUE' : 'FALSE') . "n";
echo '$zero_integer = ' . (($zero_integer == null) ? 'TRUE' : 'FALSE') . "n";
echo '$zero_numeric = ' . (($zero_numeric == null) ? 'TRUE' : 'FALSE') . "n";
echo '$negative_integer = ' . (($negative_integer == null) ? 'TRUE' : 'FALSE') . "n";
echo '$negative_numeric = ' . (($negative_numeric == null) ? 'TRUE' : 'FALSE') . "n";
echo '$array = ' . (($array == null) ? 'TRUE' : 'FALSE') . "n";
echo '$array_empty = ' . (($array_empty == null) ? 'TRUE' : 'FALSE') . "n";
echo '$true = ' . (($true == null) ? 'TRUE' : 'FALSE') . "n";
echo '$false = ' . (($false == null) ? 'TRUE' : 'FALSE') . "n";

echo "----------n";

echo "# === nulln";
echo '$null = ' . (($null === null) ? 'TRUE' : 'FALSE') . "n";
echo '$empty_string = ' . (($empty_string === null) ? 'TRUE' : 'FALSE') . "n";
echo '$string = ' . (($string === null) ? 'TRUE' : 'FALSE') . "n";
echo '$integer = ' . (($integer === null) ? 'TRUE' : 'FALSE') . "n";
echo '$numeric = ' . (($numeric === null) ? 'TRUE' : 'FALSE') . "n";
echo '$zero_integer = ' . (($zero_integer === null) ? 'TRUE' : 'FALSE') . "n";
echo '$zero_numeric = ' . (($zero_numeric === null) ? 'TRUE' : 'FALSE') . "n";
echo '$negative_integer = ' . (($negative_integer === null) ? 'TRUE' : 'FALSE') . "n";
echo '$negative_numeric = ' . (($negative_numeric === null) ? 'TRUE' : 'FALSE') . "n";
echo '$array = ' . (($array === null) ? 'TRUE' : 'FALSE') . "n";
echo '$array_empty = ' . (($array_empty === null) ? 'TRUE' : 'FALSE') . "n";
echo '$true = ' . (($true === null) ? 'TRUE' : 'FALSE') . "n";
echo '$false = ' . (($false === null) ? 'TRUE' : 'FALSE') . "n";

echo "----------n";

echo "# is_nulln";
echo '$null = ' . (is_null($null) ? 'TRUE' : 'FALSE') . "n";
echo '$empty_string = ' . (is_null($empty_string) ? 'TRUE' : 'FALSE') . "n";
echo '$string = ' . (is_null($string) ? 'TRUE' : 'FALSE') . "n";
echo '$integer = ' . (is_null($integer) ? 'TRUE' : 'FALSE') . "n";
echo '$numeric = ' . (is_null($numeric) ? 'TRUE' : 'FALSE') . "n";
echo '$zero_integer = ' . (is_null($zero_integer) ? 'TRUE' : 'FALSE') . "n";
echo '$zero_numeric = ' . (is_null($zero_numeric) ? 'TRUE' : 'FALSE') . "n";
echo '$negative_integer = ' . (is_null($negative_integer) ? 'TRUE' : 'FALSE') . "n";
echo '$negative_numeric = ' . (is_null($negative_numeric) ? 'TRUE' : 'FALSE') . "n";
echo '$array = ' . (is_null($array) ? 'TRUE' : 'FALSE') . "n";
echo '$array_empty = ' . (is_null($array_empty) ? 'TRUE' : 'FALSE') . "n";
echo '$true = ' . (is_null($true) ? 'TRUE' : 'FALSE') . "n";
echo '$false = ' . (is_null($false) ? 'TRUE' : 'FALSE') . "n";

実行結果

# DIRECT
$null = FALSE
$empty_string = FALSE
$string = TRUE
$integer = TRUE
$numeric = TRUE
$zero_integer = FALSE
$zero_numeric = FALSE
$negative_integer = TRUE
$negative_numeric = TRUE
$array = TRUE
$array_empty = FALSE
$true = TRUE
$false = FALSE

----------
# isset
$null = FALSE
$empty_string = TRUE
$string = TRUE
$integer = TRUE
$numeric = TRUE
$zero_integer = TRUE
$zero_numeric = TRUE
$negative_integer = TRUE
$negative_numeric = TRUE
$array = TRUE
$array_empty = TRUE
$true = TRUE
$false = TRUE
----------
# empty
$null = TRUE
$empty_string = TRUE
$string = FALSE
$integer = FALSE
$numeric = FALSE
$zero_integer = TRUE
$zero_numeric = TRUE
$negative_integer = FALSE
$negative_numeric = FALSE
$array = FALSE
$array_empty = TRUE
$true = FALSE
$false = TRUE
----------
# == null
$null = TRUE
$empty_string = TRUE
$string = FALSE
$integer = FALSE
$numeric = FALSE
$zero_integer = TRUE
$zero_numeric = FALSE
$negative_integer = FALSE
$negative_numeric = FALSE
$array = FALSE
$array_empty = TRUE
$true = FALSE
$false = TRUE
----------
# === null
$null = TRUE
$empty_string = FALSE
$string = FALSE
$integer = FALSE
$numeric = FALSE
$zero_integer = FALSE
$zero_numeric = FALSE
$negative_integer = FALSE
$negative_numeric = FALSE
$array = FALSE
$array_empty = FALSE
$true = FALSE
$false = FALSE
----------
# is_null
$null = TRUE
$empty_string = FALSE
$string = FALSE
$integer = FALSE
$numeric = FALSE
$zero_integer = FALSE
$zero_numeric = FALSE
$negative_integer = FALSE
$negative_numeric = FALSE
$array = FALSE
$array_empty = FALSE
$true = FALSE
$false = FALSE

あとでもう少し整理しよう・・・