# Inconsistency redesign

- Thread: https://externals.io/message/46685
- Original archive: https://news-web.php.net/php.internals/46685
- Started by mathieu.suen on 2010-01-13T13:36:36+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #46685 — mathieu.suen — 2010-01-13T13:36:36+00:00

URL: https://externals.io/message/46685#46685

Hi,

I came across this:

echo sizeof(array());
echo sizeof("");
$a = "";
var_dump( empty($a));
$a = array();
var_dump(empty($a));

So funny! How something can have a size greater than 0 but still be empty?
I think PHP is reinventing the inconsistency word.

But then let assume that empty is just making a cast in array.
$a = "";
empty($a) //true
empty((array)$a) //false

Ok so empty is big ugly switch case on type.

empty(0); //true
empty(45); //false

Wooow reinventing emptiness on number....

-- Mathieu Suen

---

## #46686 — Etienne Kneuss — 2010-01-13T13:50:17+00:00

URL: https://externals.io/message/46685#46686 · In reply to: #46685

Hello,

On Wed, Jan 13, 2010 at 2:36 PM, mathieu.suen
<mathieu.suen@easyflirt.com> wrote:
> Hi,
>
> I came across this:
>
> echo sizeof(array());
> echo sizeof("");
> $a = "";

php.net/count:
"If var is not an array or an object with implemented Countable
interface, 1 will be returned. There is one exception, if var is NULL,
0 will be returned."

> var_dump( empty($a));
> $a = array();
> var_dump(empty($a));

php.net/empty

empty($var) is basically an !isset($var) || !$var

it's not related to count in anyway.

>
> So funny! How something can have a size greater than 0 but still be empty?
> I think PHP is reinventing the inconsistency word.

I think such comments are pretty useless. Try coming with a viable
solution as a patch instead.

>
> But then let assume that empty is just making a cast in array.

If you assume wrong, you can derive all kind of madness.

> $a = "";
> empty($a) //true
> empty((array)$a) //false
>
> Ok so empty is big ugly switch case on type.

No, it's a boolean check, with type juggling involed, see above.

>
> empty(0); //true
> empty(45); //false
>
> Wooow reinventing emptiness on number....
>
> -- Mathieu Suen
>
>
>
>
> --
> 
> 
>
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #46689 — Jille Timmermans — 2010-01-13T14:00:33+00:00

URL: https://externals.io/message/46685#46689 · In reply to: #46686

var_dump((array)"");
Results in:
   array(1) {
     [0]=>
     string(0) ""
   }

And 'array("")' is not empty.

-- Jille

Op 13-1-2010 14:50, Etienne Kneuss schreef:
> Hello,
>
> On Wed, Jan 13, 2010 at 2:36 PM, mathieu.suen
> <mathieu.suen@easyflirt.com>  wrote:
> > […]
>
> php.net/count:
> "If var is not an array or an object with implemented Countable
> interface, 1 will be returned. There is one exception, if var is NULL,
> 0 will be returned."
>
> > […]
>
> php.net/empty
>
> empty($var) is basically an !isset($var) || !$var
>
> it's not related to count in anyway.
>
> > […]
>
> I think such comments are pretty useless. Try coming with a viable
> solution as a patch instead.
>
> > […]
>
> If you assume wrong, you can derive all kind of madness.
>
> > […]
>
> No, it's a boolean check, with type juggling involed, see above.
>
> > […]

---

## #46690 — mathieu.suen — 2010-01-13T14:50:18+00:00

URL: https://externals.io/message/46685#46690 · In reply to: #46686

Etienne Kneuss wrote:
> Hello,
>
> On Wed, Jan 13, 2010 at 2:36 PM, mathieu.suen
> <mathieu.suen@easyflirt.com> wrote:
>   
> > […]
>
> php.net/count:
> "If var is not an array or an object with implemented Countable
> interface, 1 will be returned. There is one exception, if var is NULL,
> 0 will be returned."
>   

The behavior might be documenting but it does not mean that's a good thing.
I don't see the point of allowing sizeof on a var that is not an array 
or object that implement countable.

>   
> > […]
>
> php.net/empty
>
> empty($var) is basically an !isset($var) || !$var
>
> it's not related to count in anyway.
>
>   
> > […]
>
> I think such comments are pretty useless. Try coming with a viable
> solution as a patch instead.
>
>   
> > […]
>
> If you assume wrong, you can derive all kind of madness.
>
>   
> > […]
>
> No, it's a boolean check, with type juggling involed, see above.
>   

Ho yes but type juggling is pretty mush a big switch case

>   
> > […]

---

## #46763 — Richard Lynch — 2010-01-17T06:17:10+00:00

URL: https://externals.io/message/46685#46763 · In reply to: #46685

Never use empty()

I've been burned by this too many times.

It's behaviour around '0' and friends changed over the years.

Some colleagues at a former job looked at me funny when they first
heard my rant about this.  Then they tracked down 3 separate bugs and
fixed them, all deriving from the use of empty().

ymmv


> Hi,
>
> I came across this:
>
> echo sizeof(array());
> echo sizeof("");
> $a = "";
> var_dump( empty($a));
> $a = array();
> var_dump(empty($a));
>
> So funny! How something can have a size greater than 0 but still be
> empty?
> I think PHP is reinventing the inconsistency word.
>
> But then let assume that empty is just making a cast in array.
> $a = "";
> empty($a) //true
> empty((array)$a) //false
>
> Ok so empty is big ugly switch case on type.
>
> empty(0); //true
> empty(45); //false
>
> Wooow reinventing emptiness on number....
>
> -- Mathieu Suen
>
>
>
>
> --
> 
> 
>
>


-- 
Some people ask for gifts here.
I just want you to buy an Indie CD for yourself:
http://cdbaby.com/search/from/lynch

---

## #46773 — Lars Schultz — 2010-01-18T07:49:16+00:00

URL: https://externals.io/message/46685#46773 · In reply to: #46763

I love empty,  because its fast and compact.

checking an array for a boolean value when a key does not exist, like here:

A:
if ( empty($foo['x']) ) echo 'no x';
this does not throw a notice that 'x' does not exist and is the fastest 
variant compared to these two, even if 'x' exists. I especially like the 
lack of ! to negate the condition.

B:
if ( !$foo['x'] ) echo 'no x';
this is simple but not faster in any case and throws a notice if it is 
not set.

C:
if ( !isset($foo['x']) || !$foo['x'] ) echo 'no x';
this is very correct but clumsy and slower than empty() in any case

as far as consistency is concerned I think that it behaves the same as B 
& C...but I may be wrong:)
PHP always had a canny behaviour when converting any value to a boolean, 
but when I got used to it and escaped the common pitfalls, I started 
liking it for it's compactness and it's naturalness.

of course, an empty string is empty(), an empty array is empty() and 
zero is empty()...naturally. The atypical behaviour that it throws a 
fatal error when using it like this:
if ( empty(bar()) )

probably stems from the fact that it's really fast;) it's not very bad 
though...
