Compare integers [closed]
$a = somenumber;
$b = somenumber;
How do I know which variable has the biggest number? What is the shorter way for this task.
Each number is a positive integer without residue.
Thanks.
max
returns the largest value from a list of values:
$biggest = max($a, $b);
If you care about which variable contains a higher value, use >
, as other answers have said.
You can use ternary operator:
$big = ($a > $b) ? $a : $b;
Kidding?
if ($a > $b) {
// $a is bigger
} else {
// $b is bigger
}
or
$bigger = max($a, $b);
function is_a_bigger($a, $b) {
return ($a - $b > 0);
}
if ($a > $b) {
echo '$a is bigger';
} else
echo '$b is bigger';
}
精彩评论