php:'0'为空字符串() [英] php: '0' as a string with empty()

查看:128
本文介绍了php:'0'为空字符串()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将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?

谢谢。

推荐答案

你做不到。这就是它的设计方式。相反,你可以写一个语句进行测试:

You cannot make 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 ,当然,除非您希望它拒绝其他空箱,检查。

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

编辑

修复了检查以进行类型检查,这要归功于2371指出的那样:)

Fixed the check to do a type check as well, thanks to the 2371 for pointing that out :)

这篇关于php:'0'为空字符串()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆