Question

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.

Was it helpful?

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?

OTHER TIPS

Use str_replace() instead of substr_replace().

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

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top