简体   繁体   中英

Comparing two integers in php

I need to compare two integers in PHP. I have this example:

$a=00713132161838;
$b=713132161838;

How can I get this comparison true? I need to delete leading zeros from $a.

I used the number_format function:

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

Why this??

leading zero to a number in php is Octal number, and it print 120370289 which is the decimal equivalent of 00713132161838 . In php there are two kinds of comparison thus,

(`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

so you can make use of single/double quote to wrap the numbers and then convert them to decimal integer value by using intval for more information you may need to read intval function in php

you can delete leading zero by ltrim

example -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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