Search code examples
phpoperatorsmismatch

PHP Datatype mismatch on comparison


So I have 2 variables, var1, var2.

$var1 = "53,000,000" //- integer
$var2 = 10 //- string

In the end I want to compare both, so I

$var1 = (int)str_replace(",","",$var1); // => 53000000 - integer

Here's my issue .. if I do:

if($var1 > $var2)  
    $var2 = $var1

I get $var2 = 0 .... Why ?
.. running on PHP 5.2.14

EDIT Accidentally typed in substr_replace instead of str_replace. Updated.


Solution

  • I had to add a couple semicolons, but here's the code:

    $var1 = "53,000,000"; //- integer
    $var2 = 10; //- string
    //In the end I want to compare both, so I
    
    $var1 = (int)str_replace(",","",$var1); // => 53000000 - integer
    //Here's my issue .. if I do:
    
    if($var1 > $var2)  
        $var2 = $var1;
    
    var_dump($var1, $var2);
    

    And here's my output:

    int(53000000) int(53000000)

    I used 5.2.6, but it shouldn't matter. Do you have any other code in between what you're showing?