Comparing String to Integer gives strange results(将字符串与整数进行比较会产生奇怪的结果)
问题描述
我真的很困惑为什么这个操作有效.有人能解释一下吗?
I'm really confused as to why this operation works. Can someone explain it?
$test1 = "d85d1d81b25614a3504a3d5601a9cb2e";
$test2 = "3581169b064f71be1630b321d3ca318f";
if ($test1 == 0)
echo "Test 1 is Equal!?";
if ($test2 == 0)
echo "Test 2 is Equal!?";
// Returns: Test 1 is Equal!?
为了澄清起见,我试图将字符串 "0" 与 $test 变量进行比较.我已经知道要修复我可以将 ""s
For clarification, I am trying to compare the string "0" to the $test variables. I already know to fix the code I can just enclose (as I should have) the 0 in ""s
我想知道这是 PHP 错误、服务器错误还是某种有效操作.根据http://us3.php.net/types.comparisons,这不应该 成功了.
I'm wondering if this is a PHP bug, a server bug, or somehow a valid operation. According to http://us3.php.net/types.comparisons this should not have worked.
从头开始,显然它确实提到字符串和 0 之间的松散比较是正确的.但我还是不知道为什么.
Scratch that, apparently it does mention that Loose comparisons between string and 0 is true. But I still don't know why.
编辑 2: 我已经修改了我的问题,为什么 "3581169b064f71be1630b321d3ca318f" 的 $test2 值不起作用?
Edit 2: I've revised my question, why does the $test2 value of "3581169b064f71be1630b321d3ca318f" not work?
推荐答案
来自 PHP 手册:
字符串转数字
当一个字符串在一个数字上下文,结果值和类型确定如下.
When a string is evaluated in a numeric context, the resulting value and type are determined as follows.
该字符串将被评估为浮动,如果它包含任何字符."、e"或E".否则,它将被评估为整数.
The string will be evaluated as a float if it contains any of the characters '.', 'e', or 'E'. Otherwise, it will be evaluated as an integer.
值由初始值给出字符串的一部分.如果字符串以有效的数字数据开头,这将是使用的值.否则,值将为 0(零).有效数字data 是一个可选的符号,后跟一位或多位数字(可选包含小数点),然后通过一个可选的指数.指数是一个 'e' 或 'E' 后跟一个或更多数字.
The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.
这篇关于将字符串与整数进行比较会产生奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将字符串与整数进行比较会产生奇怪的结果
基础教程推荐
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
