joon1
1
I have 3 numeric variables. I like to find which one is the smallest one.
I made the code below for finding the variable name which has the smallest value.
[code]if($number1 < $number2 ) {
$smaller=$number1; $smallerName=‘number1’;
} else { $smaller=$number2; $smallerName=‘number2’;}
if($number3 < $smaller) {
$smallerName=‘number3’;}
echo $smallerName;[/code]The code above works fine. but I think more simple code there in the outside.
Do you have any?
chorn
2
do you really need the name of the variable? this may cause messy code (e.g. variable variables)
$min = min($num1, $num2, $num3);
Stick them in an array and use the index number instead of worrying about the name?
Latlon
4
you can make use of arrays to declare the variables
system
Closed
5
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.