对于带有变量的多条件php-if语句,最佳实践是什么



我有一个获取内部变量的表单。原件如下:

if ($something != 'Some-thing'{
$code = ' <form id="form" method="post" action="' . $the_url . '">
</form>';
return $code;
} else { 
$code = '<form action="https://www.website.com/">
</form>';
return $code;
}

它首先检查变量$something是否等于一个值,如果是,则首先检查,如果不是,则其次检查。

但后来我想添加新的字段,使用if/else语句来切换整个字段。

现在,我已经在后端有了一个切换,我单击一个复选框,它会将$variable1更改为onoff。见下文:

后端的屏幕截图

然后我添加这个输入字段,如果它被切换,它会获取另一个字段来填充它:

<input type="hidden" name="a_variable_1" value="' . $params['a_variable_A'] . '" />

现在,我在放入if语句时遇到了问题,所以我复制了整个字段。只需要一个额外的变量就可以了。

但后来我想添加第二个带有开关切换的字段。这就是事情开始变得一团糟的地方。

这是我的代码,但它似乎不太实用。如果我想添加另一个变量,那就太荒谬了。

if ($something != 'Some-thing' && $variable1 != "on" && $variable2 != "on") {
$code = ' <form id="payfrm" method="post" action="' . $the_url . '">
</form>';
return $code;
} elseif ($something != 'Some-thing' && $variable1 != "on" && $variable2 === "on") {
$code = ' <form id="payfrm" method="post" action="' . $the_url . '">
<input type="hidden" name="variable_2" value="' . $params['variable_B'] . '" />
</form>';
return $code;
} elseif ($something != 'Some-thing' && $variable1 === "on" && $variable2 != "on") {
$code = ' <form id="payfrm" method="post" action="' . $the_url . '">
<input type="hidden" name="variable_1" value="' . $params['variable_A'] . '" />
</form>';
return $code;
} elseif ($something != 'Some-thing' && $variable1 === "on" && $variable2 === "on") {
$code = ' <form id="payfrm" method="post" action="' . $the_url . '">
<input type="hidden" name="variable_1" value="' . $params['variable_A'] . '" />
<input type="hidden" name="variable_2" value="' . $params['variable_B'] . '" />
</form>';
return $code;
} else { 
$code = '<form action="https://www.website.com/">
</form>';
return $code;
}

上面,variable_A是输入到$variable1后端的值,如上面给出的屏幕截图所示。同样,variable_B是来自后端的$variable2的文本值。

在里面添加if语句的最佳方式是什么?或者我如何修改我的代码以获得最佳实践?理想情况下,我希望在输入中添加更多带有字段的变量,而不必有混乱复杂的代码。

(快速查看)-您可以从使用常见事件减少条件树开始:

if ($something != 'Some-thing') {
if ($variable1 != "on" && $variable2 != "on") {
$code = ' <form id="payfrm" method="post" action="' . $the_url . '">
</form>';
} elseif ($variable1 != "on" && $variable2 === "on") {
$code = ' <form id="payfrm" method="post" action="' . $the_url . '">
<input type="hidden" name="a_variable_2" value="' . $params['a_variable_B'] . '" />
</form>';
} elseif ($variable1 === "on" && $variable2 != "on") {
$code = ' <form id="payfrm" method="post" action="' . $the_url . '">
<input type="hidden" name="a_variable_1" value="' . $params['a_variable_A'] . '" />
</form>';
} elseif ($variable1 === "on" && $variable2 === "on") {
$code = ' <form id="payfrm" method="post" action="' . $the_url . '">
<input type="hidden" name="a_variable_1" value="' . $params['a_variable_A'] . '" />
<input type="hidden" name="a_variable_2" value="' . $params['a_variable_B'] . '" />
</form>';
}
} else {
$code = '<form action="https://www.website.com/">
</form>';
}
return $code;

由于主要抱怨是,它变得很乱,为了减少的混乱您可以将开|关参数的值连接起来然后使用switch。类似这样的东西:

$params = "$variable1-$variable2";
switch($params) {
case 'on-on':
if($something == 'Some-thing') {
// do a
} else {
// do b
}
break;
case 'on-off':
...
}

您应该开始逐步生成代码,而不是在一个then块中生成所有内容。

首先,您必须初始化$code变量,然后当条件匹配时,您可以添加额外的代码。

最后,您只返回一个完整的代码。像这样:

$code = '';
if ($something != 'Some-thing' {
$code .= ' <form id="payfrm" method="post" action="' . $the_url . '">';
if($variable1 == 'on')
$code .= '<input type="hidden" name="variable_1" value="' . $params['variable_A'] . '" />';
if($variable2 == 'on')
$code .= '<input type="hidden" name="variable_2" value="' . $params['variable_B'] . '" />';
} else {
$code .= '<form action="https://www.thewebsiteaa.com/">';
}
$code .= '</form>';
return $code;

英语不好,我无法解释。

<input name="variable[1]" value="1">
<input name="variable[2]" value="2">
<input name="variable[3]" value="3">
...

<?php

$var_input ='';
foreach($_POST['variable'] as $key => $val){
$var_input .= sprintf('<input type="hidden" name="variable_%s" value="%s" />',$key, $val);
}
if ($something != 'Some-thing'){
$code = ' <form id="payfrm" method="post" action="' . $the_url . '">
' . $var_input . '
</form>';
return $code;
}else{
$code = '<form action="https://www.website.com/">
</form>';
return $code;
}
?>

最新更新