布尔值上的字符串比较 [英] String compare on a bool

查看:131
本文介绍了布尔值上的字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定这是我的newb PHP知识中的一个简单的基本缺陷,但是当发生以下情况时,我感到惊讶:

I'm pretty sure this is a simple fundamental flaw in my newb PHP knowledge, but I was surprised when the following happened:

$resultTRUE ...为什么将其视为等于字符串"email"?我猜这是因为,从技术上讲,这是一个bool,它不是假的吗?因此,当将其与字符串(例如"email")进行比较时,它将返回true.

$result is TRUE... so why is it considered equal to the string "email"? I'm guessing this is because, technically, it's a bool and it isn't false? So when it's compared against a string (e.g. "email") it returns true.

我应该更改方法以将返回的结果作为包含"true"的string返回(代替成功时的return true;),还是应该采用其他方法?

Should I change my method to return as the result as a string containing "true" (instead of return true; on success), or is there another way I should be doing this?

谢谢.

推荐答案

是的,true等于(==)等于非空字符串.但是不完全相同(===).

Yes, true is equal (==) to a non-empty string. Not identical (===) though.

我建议您仔细阅读类型比较表.

这篇关于布尔值上的字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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