将字符串与整数进行比较会产生奇怪的结果

2023-07-15php开发问题
22

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

问题描述

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

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.

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17