繁体   English   中英

比较PHP中的两个整数

[英]Comparing two integers in php

我需要在PHP中比较两个整数。 我有这个例子:

$a=00713132161838;
$b=713132161838;

我怎样才能使这种比较正确呢? 我需要从$ a中删除前导零。

我使用了number_format函数:

echo number_format($a,0,"","");
120370289

为什么这个??

在php中以零120370289数字是八进制数,它显示120370289 ,即120370289的十进制等效00713132161838 因此在php中有两种比较,

(`00713132161838`==713132161838) // return true
(`00713132161838`===713132161838) // return false and strict comparison
(00713132161838==713132161838) 
// this will never give you true value because 007.....8 is the octal, number first converted to decimal and comparison will be there

因此,您可以使用单引号/双引号将数字包装起来,然后使用intval将它们转换为十进制整数值,以获取更多信息,您可能需要在php中阅读intval函数

您可以按ltrim删除前导零

例子-

$str = ltrim($str, '0');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM