比较PHP中的两个整数

库法诺

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

$a=00713132161838;
$b=713132161838;

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

我使用了number_format函数:

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

为什么这个??

维斯拉吉·乔希(Veshraj Joshi)

在php中以0开头的数字是八进制数字,它打印的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函数

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章