返回变量方法
好吧,我有十个变量,但需要设置一个,但我需要检查每个值,直到设置一个值为止。我正在执行 SWITCH/CASE 语句,但我不确定这是否是最好的方法,因为我不知道如何仅返回我需要设置的变量。
$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;
}
我是不是把事情复杂化了???
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???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你在这里需要的是 $GLOBALS 数组。例如,如果您分配 $GLOBALS['var_1'] = 1,那么当您从函数返回时,变量 $var_1 将设置为 1。
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.
您无法从外部访问在函数中声明的变量。但您可以使用关键字
global
访问函数中的全局变量。但我真的不认为这就是你真正想要做的。您可能想要返回 variable 变量 或 参考。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.很难理解你想要实现什么,但正如我所见,你转储 $var_array ,你会看到类似的内容
你想根据该名称更新变量 $var_1 吗?
像这样的事情,使用 PHP 的 "variable 变量" 就可以了- 首先,您需要导出变量的名称
,然后您可以为其分配值
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
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
Then you can assign the value to it
您是第一个设置变量的人吗?因为那时您知道它是哪一个,并且您可以传回一些关于设置了哪个变量的指示,而不是向调用它的代码返回一个“谜题”。
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.