Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword empty

Test whether an expression is absent or converts to false.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

empty($row["qty"])

Test whether an expression is absent or converts to false. Missing variables or keys produce true without an undefined-variable warning. The string "0" is empty but "00" is not. A missing object property may delegate this check to __isset(). For an inaccessible property PHP first invokes __isset(). A false result makes empty true without reading. A true result invokes an available __get() to obtain the value to test.

$row=["qty"=>"0","sku"=>"00"];
echo json_encode([empty($row["missing"]),empty($row["qty"]),empty($row["sku"]),isset($row["qty"])]);
class Entries {public array $trace=[];function __isset($key){$this->trace[]="isset:$key";return $key==="on";}function __get($key){$this->trace[]="get:$key";return 2;}}
$o=new Entries();echo "\n",json_encode([empty($o->off),empty($o->on),$o->trace]);
[true,true,false,true]
[true,false,["isset:off","isset:on","get:on"]]

Examples

<?php
$row=["qty"=>"0","sku"=>"00"];
echo json_encode([empty($row["missing"]),empty($row["qty"]),empty($row["sku"]),isset($row["qty"])]);
class Entries {public array $trace=[];function __isset($key){$this->trace[]="isset:$key";return $key==="on";}function __get($key){$this->trace[]="get:$key";return 2;}}
$o=new Entries();echo "\n",json_encode([empty($o->off),empty($o->on),$o->trace]);

Result

[true,true,false,true]
[true,false,["isset:off","isset:on","get:on"]]

Sources