比较php中的两个整数


Comparing two integers in php

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

$a=00713132161838;
$b=713132161838;

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

我使用number_format函数:

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

为什么? ?

前导0的数字在php中是八进制数,并且它打印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中阅读interval函数

可以通过ltrim删除前导零

,

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