Search code examples
phpintegercomparison-operators

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??


Solution

  • 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