Erick Robertson
Erick Robertson

Reputation: 33078

How could a PHP value contain a string of zero length not equal to the empty string or null?

I am retrieving some data from an in-house store and in case of failure, I get a very specific response. Calling strlen() on this variable returns the value of zero. It is also not equal to NULL or "". I'm using this code to test:

if ($data === NULL)
{
    echo("data was null\n");
}
else if ($data === "")
{
    echo("data was empty string\n");
}
else if (strlen($data) == 0)
{
    echo("data was length zero\n");
}

This result is outputting data was length zero. What could the variable contain that is zero length, not null, and not the empty string?

Upvotes: 4

Views: 2466

Answers (3)

hek2mgl
hek2mgl

Reputation: 158010

This may not being an answer. I can only answer if you present a var_dump($data); But I think also suprising for me is this:

$data = "\0";

if ($data === NULL)
{
    echo("data was null\n");
}
else if ($data === "")
{
    echo "data was empty string\n";
}
else if (strlen($data) == 0)
{
    echo "data was length zero\n";
} 
else 
{
    echo "something strange happened\n";
}

Output: something strange happened

:)

Upvotes: 1

Dany Caissy
Dany Caissy

Reputation: 3206

Try this :

    $data = false;

I'm not sure why false has a strlen, but it does.

Upvotes: 0

Orangepill
Orangepill

Reputation: 24645

Returned value must be false then.

 echo strlen(false); // outputs 0 

Upvotes: 2

Related Questions