I have the following variables:
$a = 100;
$b = 200;
$c = 300;
Now, I'd basically like to be able to check the values of $a, $b and $c and ultimately output something like "$c is 300 and therefore the largest".
How do I achieve this using PHP?
I have the following variables:
$a = 100;
$b = 200;
$c = 300;
Now, I'd basically like to be able to check the values of $a, $b and $c and ultimately output something like "$c is 300 and therefore the largest".
How do I achieve this using PHP?
This will work even with negative values:
$a = 100;
$b = 200;
$c = -300;
$max = max($a,$b,$c);
foreach( array('a','b','c') as $v) {
if ($$v == $max) {
echo "\$$v is $max and therefore the largest";
break;
}
}
output:
$b is 200 and therefore the largest
$a = 100;
$b = 200;
$c = 300;
$max = "a"
foreach(array("a","b","c") as $v){
if($$v > $$max)$max = $v;
}
echo "$max is $$max";
Try This
$arr=array("a"=>100,"b"=>200,"c"=>300);
$val = max($arr);
print_r(array_keys($arr, $val));echo "has maximum value ".$val;
$a = 100;
$b = 200;
$c = 300;
$values = compact( 'a', 'b', 'c' );
arsort( $values );
echo '$' . key( $values ) . ' is ' . current( $values ) . ' and therefore the largest';
I like this solution as it's pretty neat; definitely aesthetically a good solution. Not sure how it would perform against other solutions on a large amount of variables.
I would definitely recommend you try and get the initial values into an array rather than separate variables to begin with.