|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-03-19 11:37 UTC] davd dot nqd at gmail dot com
Description:
------------
When applying empty() to an element of an ArrayObject which is the empty string, empty() returns true. But, the manual for empty() states that "an empty string" is considered empty.
Note: I tried to reproduce this bug using a custom implementation of ArrayAccess which worked as expected.
Reproduce code:
---------------
<?php
$array = array('index' => '');
$arrayObject = new ArrayObject($array);
if (empty($arrayObject['index'])) {
echo 'empty';
} else {
echo 'not empty';
}
?>
Expected result:
----------------
empty
Actual result:
--------------
not empty
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 18:00:02 2025 UTC |
Actually, ArrayAccess seems to be able to handle this, the following code will work as expected: <?php class demo implements ArrayAccess { public $array; function __construct() { $this->array = array('index' => '', 'index2' => 'not empty'); } function offsetExists($offset) { if (isset($this->array[$offset])) return true; else return false; } function offsetGet($offset) { if ($this->offsetExists($offset)) return $this->array[$offset]; else return (false); } function offsetSet($offset, $value) { if ($offset) $this->array[$offset] = $value; else $this->array[] = $value; } function offsetUnset($offset) { unset ($this->array[$offset]); } } $arrayObject = new demo(); if (empty($arrayObject['index'])) { echo '1'; } else { echo '0'; } if (empty($arrayObject['index2'])) { echo '1'; } else { echo '0'; } ?>