【发布时间】:2020-04-19 19:57:18
【问题描述】:
我想我会将preg_match() 的结果与(bool) 一起使用,但我不太确定。我认为不清楚结果不是true 或false。
示例 1:
if (((bool) preg_match($pattern, $string, $matches)) === true)
示例 2:
if (((bool) !preg_match($pattern, $string, $matches)) === true)
示例 3:
if (((bool) preg_match($pattern, $string, $matches)) === false)
示例 4:
if (((bool) !preg_match($pattern, $string, $matches)) === false)
另一个想法是:结果0 或1 在未来也安全吗?你有经验吗?您可以报告什么?
编辑 0:鉴于 if 没有比较运算符,问题被扩展。 0 总是 false 和 1 总是 true?
示例 5:
if ((preg_match($pattern, $string, $matches)))
示例 6:
if ((!preg_match($pattern, $string, $matches)))
这是正确的吗?(preg_match($pattern, $string, $matches)) = 0 | 1(!preg_match($pattern, $string, $matches)) = true | false
不是!
【问题讨论】:
-
为什么?只需使用
if (preg_match($pattern, $string, $matches)) { do sth } -
@WiktorStribiżew,我也有同样的想法。我有我的问题扩展这个。因为不清楚。
标签: php boolean preg-match