11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP の is_null, empty, isset, bool値 の比較表

Last updated at Posted at 2016-06-04

bool値を返す各種PHPの関数の挙動を自分のメモがてら表にまとめました。下記スクリプトで確認しています。

確認用スクリプト

is_null_table.php
<?php
class MyClass{
	function __construct(){
	}
}

$sample = array(
		'null'						=> null
		, 'true'					=> true
		, 'false'					=> false
		, '""'						=> ''
		, '"hoge"'					=> 'hoge'
		, '0'						=> 0
		, '1'						=> 1
		, '-1'						=> -1
		, 'array()'					=> array()
		, 'array(0)'				=> array(0)
		, 'array(0, 1)'				=> array(0, 1)
		, 'array("k" => null)'		=> array('k' => null)
		, 'array("k" => "v")'		=> array('k' => 'v')
		, 'MyClass'					=> new MyClass
		, 'function(){}'			=> function(){}
		, 'function($a){echo $a;}'	=> function($a){echo $a;}
	);

function output_row(){
	$width = 23;
	$separate = '|';
	$cols = func_get_args();
	$line = $separate;
	foreach ($cols as $value) {
		$space = str_repeat(' ', $width - strlen($value));
		$line .= $value . $space . $separate;
	}
	echo $line . PHP_EOL;
}
function str_true($value){
	return $value ? 'TRUE' : 'FALSE';
}

echo PHP_EOL;
echo '** True / False Table ** (PHP version ' . phpversion() . ')' . PHP_EOL . PHP_EOL;
output_row('value', 'bool', 'is_null()', 'empty()', 'isset()');
output_row('-----', '-----', '-----', '-----', '-----');
foreach ($sample as $key => $value) {
	$is_true = str_true($value);
	$is_null = str_true(is_null($value));
	$is_empty = str_true(empty($value));
	$is_set = str_true(isset($value));
	output_row($key, $is_true, $is_null, $is_empty, $is_set);
}
echo PHP_EOL;
exit(0);
?>

結果出力

結果は以下の通り。

result.txt
** True / False Table ** (PHP version 5.5.31)

|value                  |bool                   |is_null()              |empty()                |isset()                |
|-----                  |-----                  |-----                  |-----                  |-----                  |
|null                   |FALSE                  |TRUE                   |TRUE                   |FALSE                  |
|true                   |TRUE                   |FALSE                  |FALSE                  |TRUE                   |
|false                  |FALSE                  |FALSE                  |TRUE                   |TRUE                   |
|""                     |FALSE                  |FALSE                  |TRUE                   |TRUE                   |
|"hoge"                 |TRUE                   |FALSE                  |FALSE                  |TRUE                   |
|0                      |FALSE                  |FALSE                  |TRUE                   |TRUE                   |
|1                      |TRUE                   |FALSE                  |FALSE                  |TRUE                   |
|-1                     |TRUE                   |FALSE                  |FALSE                  |TRUE                   |
|array()                |FALSE                  |FALSE                  |TRUE                   |TRUE                   |
|array(0)               |TRUE                   |FALSE                  |FALSE                  |TRUE                   |
|array(0, 1)            |TRUE                   |FALSE                  |FALSE                  |TRUE                   |
|array("k" => null)     |TRUE                   |FALSE                  |FALSE                  |TRUE                   |
|array("k" => "v")      |TRUE                   |FALSE                  |FALSE                  |TRUE                   |
|MyClass                |TRUE                   |FALSE                  |FALSE                  |TRUE                   |
|function(){}           |TRUE                   |FALSE                  |FALSE                  |TRUE                   |
|function($a){echo $a;} |TRUE                   |FALSE                  |FALSE                  |TRUE                   |

ちょっと考察

is_null()は値がnullのときだけtrueで、isset()はその否定になっているみたいですね。一方、empty()bool評価の否定になっている模様。

以上です。

11
11
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?