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

2019-08-02 18:30发布

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

标签: php post forms
3条回答
够拽才男人
2楼-- · 2019-08-02 19:05
$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)
查看更多
forever°为你锁心
3楼-- · 2019-08-02 19:14

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.

查看更多
▲ chillily
4楼-- · 2019-08-02 19:22

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.

查看更多
登录 后发表回答