PHP 的 preg_match() 行为怪异
PHP's preg_match() acts weirdly
我在 PHP 中进行正则表达式验证时发现了这个。
我已经使用 preg_match
函数来验证字符串。
我刚刚打印了由 preg_match
编辑的值 return 以确保工作流程。
在那期间我发现了一件事。但是看不懂他们的概念。
如果找到匹配,那是 preg_match()
returns 1。否则,它将 return 0。但有时我的打印功能没有打印任何东西。
在我通过 PHP 手册了解其所有 return 价值之后...
他们在那里张贴如下...
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
然后我使用 var_dump
知道是否打印了任何内容...
这让我知道这是一个布尔值 false。
但我很想知道为什么它 return 是布尔值 false,而我只是在 preg_match()
?
之前放置“!”(不是)
以下是我的代码,
echo preg_match("/script/", "script") // ===> this returns 1
echo !preg_match("/script/", "script") // ===> this returns Boolean false
我认为它必须 return 整数 0(零)...它的功能是什么?还是我在语法上做错了什么?
我已经在 OpenCart 2.0.0.0 系统管理员控制器模块中试过了。
当您在函数前面放置 !
时,您并没有回显 preg_match 的 return 值。您正在使用 PHP 的运算符来确定它的粗略计算结果是真还是假。
!preg_match("/script/","script")
与 preg_match("/script/","script") == false
相同。请注意 ==
而不是 ===
。 0、null、空字符串和布尔值 false 将松散地评估为 false。
!
运算符总是 return 布尔值。
For a unary ! operator the type of the result is bool. The value of the operand is converted to type bool and if it is TRUE then the of the operator result is FALSE, and the result is TRUE otherwise.
语言规范参考:Expressions
bool
的正常转换规则应用于该值。
我在 PHP 中进行正则表达式验证时发现了这个。
我已经使用 preg_match
函数来验证字符串。
我刚刚打印了由 preg_match
编辑的值 return 以确保工作流程。
在那期间我发现了一件事。但是看不懂他们的概念。
如果找到匹配,那是 preg_match()
returns 1。否则,它将 return 0。但有时我的打印功能没有打印任何东西。
在我通过 PHP 手册了解其所有 return 价值之后...
他们在那里张贴如下...
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
然后我使用 var_dump
知道是否打印了任何内容...
这让我知道这是一个布尔值 false。
但我很想知道为什么它 return 是布尔值 false,而我只是在 preg_match()
?
以下是我的代码,
echo preg_match("/script/", "script") // ===> this returns 1
echo !preg_match("/script/", "script") // ===> this returns Boolean false
我认为它必须 return 整数 0(零)...它的功能是什么?还是我在语法上做错了什么?
我已经在 OpenCart 2.0.0.0 系统管理员控制器模块中试过了。
当您在函数前面放置 !
时,您并没有回显 preg_match 的 return 值。您正在使用 PHP 的运算符来确定它的粗略计算结果是真还是假。
!preg_match("/script/","script")
与 preg_match("/script/","script") == false
相同。请注意 ==
而不是 ===
。 0、null、空字符串和布尔值 false 将松散地评估为 false。
!
运算符总是 return 布尔值。
For a unary ! operator the type of the result is bool. The value of the operand is converted to type bool and if it is TRUE then the of the operator result is FALSE, and the result is TRUE otherwise.
语言规范参考:Expressions
bool
的正常转换规则应用于该值。