PHP.nl

empty

empty

Determine whether a variable is empty

bool **empty** mixed $var

Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals false. does not generate a warning if the variable does not exist. empty

varVariable to be checked

   No warning is generated if the variable does not exist.
   That means  is essentially the
   concise equivalent to .
   This also applies to nested structures, such as a multidimensional array or chained properties.
  `empty`

Returns true if does not exist or has a value that is empty or equal to zero, aka falsey, see . Otherwise returns false. varconversion to boolean

**Voorbeeld: A simple / comparison. **

<?php
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}
?>

Voorbeeld: on String Offsets

<?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)

Voorbeeld: on multidimensional arrays

<?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)

Opmerking: > When using on inaccessible object properties, the overloading method will be called, if declared. empty__isset()

isset__isset()unset``array_key_exists``count``strlenThe type comparison tables