将字符串与整数进行比较会产生奇怪的结果 [英] Comparing String to Integer gives strange results

查看:44
本文介绍了将字符串与整数进行比较会产生奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑为什么这个操作有效.有人能解释一下吗?

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.

这篇关于将字符串与整数进行比较会产生奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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