簡體   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