本文介绍了在PHP中将'0'作为带有empty()的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,我希望将0视为整数,将'0'视为字符串,但是empty()会将'0'视为字符串,

I want a 0 to be considered as an integer and a '0' to be considered as a string, but empty() considers the '0' as a string in the example below,

$var = '0';

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is empty';
}

我怎样才能使empty()取为字符串的"0"?

How can I 'make' empty() to take '0's as strings?

推荐答案

您不能使empty()接受它.那就是它的设计方式.相反,您可以编写语句进行测试:

You cannot make empty() take it. That is how it was designed. Instead you can write an and statement to test:

if (empty($var) && $var !== '0') {
    echo $var . ' is empty';
}

您可以使用 isset ,除非您当然希望它拒绝 empty 检查的其他容器.

You could use isset, unless of course, you want it to turn away the other empties that empty checks for.

这篇关于在PHP中将'0'作为带有empty()的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:37