Is there any difference between {strlen($var) == 0

2019-08-02 18:45发布

问题:

considering that the variable only can be String fetched from an HTML Form (input-text, textarea).

回答1:

Yes, there is a difference between strlen($str)==0 and empty($str). empty returns true if the value is "0". See the PHP type comparison tables.



回答2:

$var = 0;

strlen( $var ); // 1, coerced to true

empty($var) // true, it's considered "empty", these are the empty ones:
  • "" (an empty string)
  • 0 (0 as an integer)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)


回答3:

Here is some note I've discovered:
empty(), requires a variable and only a variable, so I think it has a problem when it's dealing with an object value fetched from the magic __get() method.



标签: php post forms