Adrian A.
Adrian A.

Reputation: 172

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.

Upvotes: 0

Views: 293

Answers (4)

Parris Varney
Parris Varney

Reputation: 11478

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?

Upvotes: 1

Ravindran
Ravindran

Reputation: 1

No need for type casting. just do the str_replace

Here is code

$var1 = "53,000,000" ;    
$var2 = 10;  
$var1=str_replace(',','',$var1);  
if($var1 > $var2)    
    $var2 = $var1;

    echo $var2;

Upvotes: 0

acme
acme

Reputation: 14856

You've specified the wrong parameters for substr_replace, so $var1 is evaluated to 0. I guess you wanted to use str_replace.

Upvotes: 0

pestaa
pestaa

Reputation: 4749

Use str_replace() instead of substr_replace().

Upvotes: 0

Related Questions