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

StackOverflow https://stackoverflow.com/questions/17604433

  •  02-06-2022
  •  | 
  •  

Question

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?

Was it helpful?

Solution

Returned value must be false then.

 echo strlen(false); // outputs 0 

OTHER TIPS

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

:)

Try this :

    $data = false;

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top