-4

Any direct method to get previous variable name ?

$a = "";
$b = "";
if (logic) {
    $result = $a;
} elseif (logic) {
    $result = $b;
}

any direct_function($result) can get "a" or "b" ?

Many thanks.

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
Thomas Lee
  • 83
  • 1
  • 6
  • Not getting your question. Can you explain briefly? – Vipin CP Aug 03 '16 at 04:33
  • When/why is `$result` a or b? Ternary seems like an option. – chris85 Aug 03 '16 at 04:34
  • 1
    i don't know if there's a function that really does this, you want to get the variable name of the last assignment of that main variable. good question though, or even to check whether the assignment is any value, or a variable or whatever, any particular reason why you need this? – Kevin Aug 03 '16 at 04:36
  • i think you don't even know what you want, please be specific – Amrinder Singh Aug 03 '16 at 04:41
  • This question makes absolutely no sense. Can you please explain differently – Darren H Aug 03 '16 at 04:49
  • maybe related http://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php – Kevin Aug 03 '16 at 05:10

1 Answers1

1

If i get your question then i think the Best way is to use an array().
For example:

$a = "";
$b = "";
$result=array();     // an empty array
if (logic) {
    $result[] = $a;
} elseif (logic) {
    $result[] = $b;
}

Now you can get any value by index.

Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
  • 1
    I don't understand why this would be any different to what he has. Why is an array useful in this situation? I'm not convinced any of us actually understand the question though... (including the OP) – Darren H Aug 03 '16 at 04:50