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

I retrieve some data from my own store, and in case of failure I get a very specific answer. Calling strlen() for this variable returns 0. It is also not NULL or "". I use this code to check:

 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 outputs data was length zero . What can a variable contain: zero length, not zero, not an empty string?

+4
source share
3 answers

The return value must be false.

  echo strlen(false); // outputs 0 
+2
source

This may not be the answer. I can only answer if you represent var_dump($data); . But I think this is also surprising for me:

 $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"; } 

Exit: something strange happened

:)

+1
source

Try the following:

  $data = false; 

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

0
source

Source: https://habr.com/ru/post/1491020/


All Articles