views:

64

answers:

7

Hi

    $var1 = 22;
    $var2 = 10;
    echo $var1 = ($var1 < $var2) ? $var1 : $var2; //smaller var
    echo '<br />';
    echo $var2 = ($var1 > $var2) ? $var1 : $var2; //greater var

I expect it to print 10 and 22 but it prints 10 and 10. any ideas what I am doing wrong?

Thanks

UPDATE Thanks all.

    $min = min($var1, $var2); 
    $max = max($var1, $var2);

    $var1 = $min;
    $var2 = $max;
+1  A: 

You overwrite $var1 in your first comparison. So the second comparison compares 10 > 10.

$var1 = 22;
$var2 = 10;
echo $var1 = (10 < 22) ? 22 : 10; //smaller var -> $var1 now has the value 10
echo '<br />';
echo $var2 = (10 > 10) ? 22 : 10; //greater var -> 10 is not greater than 10, so $var2 gets a value of 10.
Ham
+4  A: 

You are re-assigning the variables in the echo.

// $var1 is being assigned minimum of 10,22 which is 10.
// after this $var1 and $var2 will both be 10.
echo $var1 = ($var1 < $var2) ? $var1 : $var2; 

What you want it:

echo ($var1 < $var2) ? $var1 : $var2; // prints min.
echo '<br />';
echo ($var1 > $var2) ? $var1 : $var2; // prints max.

EDIT:

If you always want the smaller of the two values in $var1 you can do:

if($var1 > $var2) { // if $var1 is larger...swap.
        list($var1,$var2)  = array($var2,$var1);
}
codaddict
@Downvoter: Care to explain ?
codaddict
I didn't downvote, but... Instead of the `if` around the swap, you can use `list($var1,$var2) = array(min($var1, $var2), max($var1, $var2))`. Of course, that will then *always* do it, but it's not a very expensive operation.
nikc
@nikc why make things more complicated than they need to be; if the values are already in their correct variables, why bother doing the work?
salathe
I don't think it complicates things, it just might do a few unnecessary operations. But my intention was just to provide an alternative solution.
nikc
+1  A: 

you assign 10 to $var1 with the first echo, so at the second they are both 10.

Lo'oris
+1  A: 
echo $var1 = ($var1 < $var2) ? $var1 : $var2; //smaller var

This assigns 10 to $var1. Now both variables contain 10. So what do you expect of the second line?

Amarghosh
+1  A: 

You need a temporary variable. Just use min,

echo min($var1, $var2);
ZZ Coder
+4  A: 

@unicornaddict already solved your problem, but to make it simpler you can use the min and max functions PHP provides.

echo min($var1, $var2), '<br/>', max($var1, $var2);
nikc
it still prints 10 and 10.. see ques update. thx
Sorry did the same its ok now. thx
+1  A: 

Your question also mentions swapping the values, which the other answers don't seem to make any note of. Given your example code it looks like you want $var1 to contain the smaller of the two values and $var2 the bigger.

$var1 = 22;
$var2 = 10;
if ($var1 > $var2) {
    list($var1, $var2) = array($var2, $var1);
}
// $var1 will now be smaller than (or equal to!) $var2
salathe
@Downvoters, care to explain why? The swapping was always part of the question (and still is) which others had not addressed.
salathe