views:

73

answers:

4

Ok I have ten variables but one needs to be set, but I need to check for each of the values until one is set. I'm doing a SWITCH/CASE statement but I'm not sure if this is the best approach because I don't see how I can return only the variable that I need set.

$passed_var = 'A'; // Static

$var_array = getSelectedVar($passed_var);

foreach($var_array as $key => $val) {
    echo "Key: " . $key . " VALUE: " . $val . "<br />\n";
}

// How do I assign one of these values?
if(isset($var_1)) {
    echo "Var 1 Set<br />\n";
} elseif(isset($var_2)) {
    echo "Var 2 Set<br />\n";
} elseif(isset($var_3)) {
    echo "Var 3 Set<br />\n";
} elseif(isset($var_4)) {
    echo "Var 4 Set<br />\n";
} elseif(isset($var_5)) {
    echo "Var 5 Set<br />\n";
} elseif(isset($var_6)) {
    echo "Var 6 Set<br />\n";
} elseif(isset($var_7)) {
    echo "Var 7 Set<br />\n";
} elseif(isset($var_8)) {
    echo "Var 8 Set<br />\n";
} elseif(isset($var_9)) {
    echo "Var 9 Set<br />\n";
} elseif(isset($var_10)) {
    echo "Var 10 Set<br />\n";
}

function getSelectedVar($passed_var) {
    $passed_var = strtoupper($passed_var);
    $update_var = array();

    switch ($passed_var) {
        case 'A':
        case 'AB':
        case 'ABC':
        case 'ABCD':
            $update_var_1 = 1;
            $update_var = array('update_var_1'=>'1');
            break;
        case 'B':
        case 'BA':
            $update_var_2 = 1;
            $update_var = array('update_var_2'=>'1');
            break;
        case 'C':
            $update_var_3 =1;
            $update_var = array('update_var_3'=>'1');
            break;
        case 'D':
        case 'DA':
            $update_var_4 =1;
            $update_var = array('update_var_4'=>'1');
            break;
        case 'E':
            $update_var_5 =1;
            $update_var = array('update_var_5'=>'1');
            break;
        case 'F':
            $update_var_6 =1;
            $update_var = array('update_var_6'=>'1');
            break;
        case 'G':
            $update_var_7 = 1;
            $update_var = array('update_var_7'=>'1');
            break;
        case 'H':
        case 'HA':
        case 'HB':
            $update_var_8 =1;
            $update_var = array('update_var_8'=>'1');
            break;
        case 'I':
            $update_var_9 =1;
            $update_var = array('update_var_9'=>'1');
            break;
        case 'J':
        case 'JA':
            $update_var_10 =1;
            $update_var = array('update_var_10'=>'1');
            break;
        default:
            $update_var = '';
    }

    return $update_var;
}

Am I over complicating things???

A: 

You can't access variables that you declare in the function from outside. But you can use the keyword global to access global variables in your function. But I really don't think this is what you actually want to do. You might either want to return a variable variable or a reference.

 // Variable variable example:
 $var_1 = 'test';
 $a = 'var_1';
 echo $$a; // outputs 'test'

 // Reference example:
 $var_1 = 'test';
 $a = &$var_1;
 $a = 'test2';
 echo $var_1; // outputs 'test2'
soulmerge
I think this is what I'm looking for, let me test; // Variable variable example: $var_1 = 'test'; $a = 'var_1'; echo $$a; // outputs 'test'
Phill Pafford
A: 

It's hard to understand what you want to achieve, but as I see it, you dump out your $var_array and you'll see something like

Key: update_var_1 VALUE: 1

And you want to update the variable $var_1 based on that name?

Something like this, using PHP's "variable variables" would work - first, you need to derive the name of the variable

$varname=str_replace("update_", "", $key);

Then you can assign the value to it

$$varname=$value
Paul Dixon
+1  A: 

I think what you need here is the $GLOBALS array. For Example, if you assign $GLOBALS['var_1'] = 1 then when you return from the function, the variable $var_1 will be set to 1.

Tom
Thanks, I think this is the simplest approach
Phill Pafford
A: 

Are you the one setting the variable in the first place? Because at that point you know which one it is, and you could pass back some indication as to which variable got set, instead of returning a "puzzle" to the code that called it.

gbarry