Skip to content

Is empty("0") => true really intended? #17562

@m23project

Description

@m23project

Description

Hi,

I wondered about the result of checking the string "0" with the function empty.

The string isn't empty obviously, but as "empty" is a short version of

!isset($var) || $var == false

. "0" is casted to false. This is as defined, but astonishing, if you take the word empty verbatim.

Is it really the result developers expect, when checking a variable containing the string "0"?

The following code:

<?php
var_dump(empty("0"));
?>

Resulted in this output:

bool(true)

But I expected this output instead:

bool(false)

PHP Version

PHP 8.2.26

Operating System

Debian 12

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions