1

I'm learning validation stuff, and I just can't understand this:

if (strpos($value, "@") === false) { echo "Validation failed." }

What's the difference between === and ==? and why can't we use == instead and also why is it === false? does false means that @ is not in the $value or it means 0 ?

4

1 回答 1

0

Equality Operator==
A == B检查 A 和 B 是否相等,但不检查它们是否是相同的数据类型。

一个相关的例子:0 == false是真的

Identity Operator===
A === B检查 A 和 B 是否相等以及相同的数据类型。

一个相关的例子:0 === false是假的

在这里申请

将此应用于您的案例,如果@发现 是字符串的第一个字符,strpos($value,"@")将返回0. 如果根本找不到,它将返回false

所以为了避免混淆这两种情况,测试必须使用===而不是==.

有用的参考资料:

http://php.net/manual/en/function.strpos.php http://php.net/manual/en/language.operators.comparison.php

我假设这是 php,但等式和身份运算符对许多编程语言来说很常见。

于 2017-05-07T13:53:27.437 回答