empty

(PHP 4, PHP 5, PHP 7, PHP 8)

empty変数が空であるかどうかを検査する

説明

function empty(mixed $var): bool

変数が空であるかどうかを検査します。 変数が空であるとみなされるのは、変数が存在しない場合や 変数の値が false に等しい場合です。 empty() は、変数が存在しない場合でも警告を発生させません。 このことは、多次元配列やチェインされたプロパティのような、 ネストされたデータ構造であっても当てはまります。

パラメータ

var

チェックする変数

変数が存在しなくても警告は発生しません。 つまり、empty() は本質的に !isset($var) || $var == false と同じことを簡潔に記述しているだけです。

戻り値

var が存在しない、 または空や0の値が設定されている場合、 つまり boolean のコンテキストで false と見なされる場合、 true を返します。 詳細は boolean への変換 を参照ください。 それ以外の場合は false を返します。

例1 簡単な empty() / isset() の比較

<?php
$var = 0;

// $var が空なのでtrueと評価されます
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// $var が設定されているのでtrueと評価されます
if (isset($var)) {
    echo '$var is set even though it is empty';
}
?>

例2 文字列のオフセットに対する empty()

<?php
$expected_array_got_string = 'somestring';
var_dump(empty($expected_array_got_string['some_key']));
var_dump(empty($expected_array_got_string[0]));
var_dump(empty($expected_array_got_string['0']));
var_dump(empty($expected_array_got_string['0.5']));
var_dump(empty($expected_array_got_string['0 Mostel']));
?>

上の例の出力は以下となります。

bool(true)
bool(false)
bool(false)
bool(true)
bool(true)

例3 多次元配列に対する empty()

<?php
$multidimensional = [
    'some' => [
        'deep' => [
            'nested' => 'value'
        ]
    ]
];

if (!empty($multidimensional['some']['some']['nested'])) {
    $someVariable = $multidimensional['some']['deep']['nested'];
}

var_dump(empty($multidimensional['some-undefined-key']));
var_dump(empty($multidimensional['some']['deep']['unknown']));
var_dump(empty($multidimensional['some']['deep']['nested']));
?>

上の例の出力は以下となります。

bool(true)
bool(true)
bool(false)

注意

注意:

これは、関数ではなく 言語構造のため、可変関数名前付き引数 を用いてコールすることはできません。

注意:

オブジェクトのアクセス不能なプロパティに対して empty() を使用した場合は、もしオーバーロードメソッド __isset() が宣言されていればそれをコールします。

参考

  • isset() - 変数が宣言されていること、そして null とは異なることを検査する
  • __isset()
  • unset() - 指定した変数の割当を解除する
  • array_key_exists() - 指定したキーまたは添字が配列にあるかどうかを調べる
  • count() - 配列または Countable オブジェクトに含まれるすべての要素の数を数える
  • strlen() - 文字列の長さを得る
  • PHP 型の比較表

add a note

User Contributed Notes 36 notes

up
276
Nanhe Kumar
12 years ago
<?php
/**
* @author :  Nanhe Kumar <nanhe.kumar@gmail.com>
* List of all empty values
**/

$testCase = array(
    1 => '',
    2 => "",
    3 => null,
    4 => array(),
    5 => FALSE,
    6 => NULL,
    7=>'0',
    8=>0,
    
);

foreach ($testCase as $k => $v) {
    if (empty($v)) {
        echo "<br> $k=>$v is empty";
    }
}
/**
Output
1=> is empty
2=> is empty
3=> is empty
4=>Array is empty
5=> is empty
6=> is empty
7=>0 is empty
8=>0 is empty
**/
?>
up
95
Janci
17 years ago
Please note that results of empty() when called on non-existing / non-public variables of a class are a bit confusing if using magic method __get (as previously mentioned by nahpeps at gmx dot de). Consider this example:

<?php
class Registry
{
    protected $_items = array();
    public function __set($key, $value)
    {
        $this->_items[$key] = $value;
    }
    public function __get($key)
    {
        if (isset($this->_items[$key])) {
            return $this->_items[$key];
        } else {
            return null;
        }
    }
}

$registry = new Registry();
$registry->empty = '';
$registry->notEmpty = 'not empty';

var_dump(empty($registry->notExisting)); // true, so far so good
var_dump(empty($registry->empty)); // true, so far so good
var_dump(empty($registry->notEmpty)); // true, .. say what?
$tmp = $registry->notEmpty;
var_dump(empty($tmp)); // false as expected
?>

The result for empty($registry->notEmpty) is a bit unexpeced as the value is obviously set and non-empty. This is due to the fact that the empty() function uses __isset() magic functin in these cases. Although it's noted in the documentation above, I think it's worth mentioning in more detail as the behaviour is not straightforward. In order to achieve desired (expexted?) results, you need to add  __isset() magic function to your class:

<?php
class Registry
{
    protected $_items = array();
    public function __set($key, $value)
    {
        $this->_items[$key] = $value;
    }
    public function __get($key)
    {
        if (isset($this->_items[$key])) {
            return $this->_items[$key];
        } else {
            return null;
        }
    }
    public function __isset($key)
    {
        if (isset($this->_items[$key])) {
            return (false === empty($this->_items[$key]));
        } else {
            return null;
        }
    }
}

$registry = new Registry();
$registry->empty = '';
$registry->notEmpty = 'not empty';

var_dump(empty($registry->notExisting)); // true, so far so good
var_dump(empty($registry->empty)); // true, so far so good
var_dump(empty($registry->notEmpty)); // false, finally!
?>

It actually seems that empty() is returning negation of the __isset() magic function result, hence the negation of the empty() result in the __isset() function above.
up
70
steven at nevvix dot com
15 years ago
When you need to accept these as valid, non-empty values:
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)

<?php
function is_blank