-1

I need to select the largest value between two variables, but not return the value itself. So using max won't do the trick.

Example:

$value1 = 60;  
$value2 = 24;

I need $value1 (the variable name) to be returned. This would be simpler to do, were the variables in an array as I could do a foreach loop to return the key with the highest value, but I don't think there's much point creating an array when there's only every going to be two values, unless it won't effect the efficiency at all (I'm not sure it will).

Ashrith Sheshan
  • 654
  • 4
  • 17
Karl
  • 5,435
  • 11
  • 44
  • 70
  • 2
    This makes no sense. You can't "return a variable"; do you mean, you want to return the *name* of a variable, as in a string? Or a reference to the variable? Show some real code, instead of simply saying "i need Value1 to be returned". – user229044 Jun 27 '13 at 14:58
  • Can you provide data type? Some example code you've tried? – Rob W Jun 27 '13 at 14:58
  • Wait. "I need to select the largest value between two variables, **but not return the value itself.**" but then "I need Value1 to be returned." Which is it? Although I'm not sure how you want to get the maximum value if the function doesn't return it like `max()`. – Bailey Parker Jun 27 '13 at 14:58
  • but where do you have the variables listed to see them? – Tony Jun 27 '13 at 15:00
  • if you know there are only going to be 2 values, can't you just use an if statement? – Andrew Morris Jun 27 '13 at 15:00
  • 1
    Take a look a this http://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php – Pitchinnate Jun 27 '13 at 15:00
  • you mean the name of the variable??? – Tony Jun 27 '13 at 15:01
  • 1
    Obviously I meant the name of the variable, that's why you all keep asking the same question. If everyone is asking it, surely that's just common logic? – Karl Jun 27 '13 at 15:05
  • 2
    @Karl, `$ans = $value1 > $value2 ? '$value1' : '$value2';`? – Dogbert Jun 27 '13 at 15:09

1 Answers1

0
$biggerValue = null;
if ($value1 > $value2){
  $biggerValue = 'value1';
} elseif ($value2 > $value1) {
  $biggerValue = 'value2';
}
GolezTrol
  • 114,394
  • 18
  • 182
  • 210