PHP 変数のチェック用関数について

Last Update 2012-04-09

HomeBack

  1. 結果
  2. ソース

結果

変数が null や "" の場合に、if文や、empty()、 isset() などの関数で、どのような結果を返すかを調べてみました。

以下はWindowsXP、 PHP 5.2.8 で試した結果で、間違いそうなところを赤色にしています。

> php -f test_empty.php
$a = null
empty($a)   => true
isset($a)   => false
is_null($a)   => true
is_array($a)   => false
if ($a == "")   => true
if ($a === "")   => false
if ($a == null)   => true
if ($a === null)   => true

$a = ""
empty($a)   => true
isset($a)   => true
is_null($a)   => false
is_array($a)   => false
if ($a == "")   => true
if ($a === "")   => true
if ($a == null)   => true
if ($a === null)   => false

$a = " "
empty($a)   => false
isset($a)   => true
is_null($a)   => false
is_array($a)   => false
if ($a == "")   => false
if ($a === "")   => false
if ($a == null)   => false
if ($a === null)   => false

$a = Array()
empty($a)   => true
isset($a)   => true
is_null($a)   => false
is_array($a)   => true
if ($a == "")   => false
if ($a === "")   => false
if ($a == null)   => true
if ($a === null)   => false

$a = Array("")
empty($a)   => false
isset($a)   => true
is_null($a)   => false
is_array($a)   => true
if ($a == "")   => false
if ($a === "")   => false
if ($a == null)   => false
if ($a === null)   => false

$ar = Array("a" => "AA", "b" => "", "c" => null)
array_key_exists("b", $a)  => true
isset($ar["b"])  => true
empty($ar["b"])  => true

array_key_exists("c", $a)  => true
isset($ar["c"])  => false
empty($ar["c"])  => true

array_key_exists("e", $a)  => false
isset($ar["e"])  => false
empty($ar["e"])  => true

ソース

テストに使用したプログラムのソースです。

<?php
class EmptyTest {
    private $needBr = false;

    // テスト処理
    public function test() {
        $this->testNull();
        $this->testKara();
        $this->testSpace();
        $this->testEmptyArray();

        $this->testArray();
    }

    private function println($s) {
        if ($this->needBr) {
            echo $s . "<br />\n";
        } else {
            echo $s . "\n";
        }
    }

    // true をセットすると、 println() で<br /> 
    // を付けて出力する
    public function setNeedBr($b) {
        $needBr = $b;
    }

    private function testNull() {
        $this->println('$a = null');
        $a = null;
        $this->printTestVar($a);
        $this->println('');
    }
    
    private function testKara() {
        $this->println('$a = ""');
        $a = '';
        $this->printTestVar($a);
        $this->println('');        
    }

    private function testSpace() {
        $this->println('$a = " "');
        $a = "  ";
        $this->printTestVar($a);
        $this->println('');
    }
    
    private function testEmptyArray() {
        $this->println('$a = Array()');
        $a = Array();
        $this->printTestVar($a);
        $this->println('');
        
        $this->println('$a = Array("")');
        $a = Array("");
        $this->printTestVar($a);
        $this->println('');
    }
    
    private function testArray() {
        $this->println('$ar = Array("a" => "AA", "b" => "", "c" => null)');
        $ar["a"] = "AA";
        $ar["b"] = "";
        $ar["c"] = null;
        $this->printTestArray($ar, "b");
        $this->println("");
        $this->printTestArray($ar, "c");
        $this->println("");
        $this->printTestArray($ar, "e");
        $this->println("");        
    }

    private function printTestVar($a) {
        
        if (empty($a)) {
            $this->println('empty($a)   => true');
        } else {
            $this->println('empty($a)   => false');
        }
        
        if (isset($a)) {
            $this->println('isset($a)   => true');
        } else {
            $this->println('isset($a)   => false');
        }
                
        if (is_null($a)) {
            $this->println('is_null($a)   => true');
        } else {
            $this->println('is_null($a)   => false');
        }

        if (is_array($a)) {
            $this->println('is_array($a)   => true');
        } else {
            $this->println('is_array($a)   => false');
        }
        
        if ($a == "") {
            $this->println('if ($a == "")   => true');
        } else {
            $this->println('if ($a == "")   => false');
        }
        if ($a === "") {
            $this->println('if ($a === "")   => true');
        } else {
            $this->println('if ($a === "")   => false');
        }
    
        if ($a == null) {
            $this->println('if ($a == null)   => true');
        } else {
            $this->println('if ($a == null)   => false');
        }
        if ($a === null) {
            $this->println('if ($a === null)   => true');
        } else {
            $this->println('if ($a === null)   => false');
        }
    }
    
    private function printTestArray($ar, $keyName) {
        if (is_array($ar) != true) {
            $this->println("invalid array");
            var_dump($ar);
        }
        if (array_key_exists($keyName, $ar)) {
            $this->println('array_key_exists("' . $keyName . '", $a)  => true');
        } else {
            $this->println('array_key_exists("' . $keyName . '", $a)  => false');
        }
        if (isset($ar[$keyName])) {
            $this->println('isset($ar["' . $keyName . '"])  => true');
        } else {
            $this->println('isset($ar["' . $keyName . '"])  => false');
        }
        if (empty($ar[$keyName])) {
            $this->println('empty($ar["' . $keyName . '"])  => true');
        } else {
            $this->println('empty($ar["' . $keyName . '"])  => false');
        }
    }
}

$obj = new EmptyTest();
$obj->test();


HomeBack