且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么检查字符串是否为空的函数总是返回 true?

更新时间:2021-10-06 07:23:59

其实很简单的问题.更改:

Simple problem actually. Change:

if (strTemp != '')

if ($strTemp != '')

可以说,您可能还想将其更改为:

Arguably you may also want to change it to:

if ($strTemp !== '')

因为 != '' 将返回 true 如果您传递的是数字 0 和其他一些由于 PHP 的自动类型转换.

since != '' will return true if you pass is numeric 0 and a few other cases due to PHP's automatic type conversion.

不应该使用内置的 empty() 函数这;请参阅评论和 PHP 类型比较表.

You should not use the built-in empty() function for this; see comments and the PHP type comparison tables.