Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带变量的多条件php if语句的最佳实践是什么?_Php_Variables_If Statement - Fatal编程技术网

带变量的多条件php if语句的最佳实践是什么?

带变量的多条件php if语句的最佳实践是什么?,php,variables,if-statement,Php,Variables,If Statement,我有一个获取内部变量的表单。原版看起来像这样: if ($something != 'Some-thing'{ $code = ' <form id="form" method="post" action="' . $the_url . '"> </form>'; return $code; } else { $code = '<form action="https://www.website.com/">

我有一个获取内部变量的表单。原版看起来像这样:

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;
}
$params = "$variable1-$variable2";

switch($params) {
  case 'on-on':
    if($something == 'Some-thing') {
      // do a
    } else {
      // do b
    }
    break;
  case 'on-off':
    ...
}
现在,我很难将
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;
}
if($something!=“something”&&&$variable1!=“on”&&&$variable2!=“on”){
$code='1
';
返回$code;
}elseif($something!=“something”&&&$variable1!=“on”&&&$variable2==“on”){
$code='1
';
返回$code;
}elseif($something!=“something”&&&$variable1==“on”&&&$variable2!=“on”){
$code='1
';
返回$code;
}elseif($something!=“something”&&&$variable1==“on”&&&$variable2==“on”){
$code='1
';
返回$code;
}否则{
$code='1
';
返回$code;
}
上面,
variable\u 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;
if($something!=“something”){
如果($variable1!=“on”&&$variable2!=“on”){
$code='1
';
}elseif($variable1!=“on”&&$variable2==“on”){
$code='1
';
}elseif($variable1==“开”&&$variable2!=“开”){
$code='1
';
}elseif($variable1==“on”&&$variable2==“on”){
$code='1
';
}
}否则{
$code='1
';
}
返回$code;

您应该开始一步一步地生成代码,而不是在一个
然后
-块中生成所有内容

首先必须初始化
$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;
$code='';
如果($something!=“something”{
$code.='';
如果($variable1=='on')
$code.='';
如果($variable2=='on')
$code.='';
}否则{
$code.='';
}
$code.='';
返回$code;

由于主要的抱怨是它变得杂乱无章,为了减少杂乱无章,您可以将on | off参数的值串联起来,然后使用一个
开关。类似如下:

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;
}
$params = "$variable1-$variable2";

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

英语不好,我无法解释

<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;
}

?>

...

为什么您必须在服务器端发出这么多的标记?当您使用
return
时,您可以从
else
中解放出来,这样您就可以执行:
if(cond1){return'string
';}if(cond2){return string2';}…`。哦,我想知道我是否可以这样做。所以如果我这样做了,并且有多个
$code
,当我在末尾调用
$code
时,这将按顺序返回所有行?还有为什么
=
符号前有一个点?这是连接
$code
的所有实例所需要的吗?是的。重要的是,这是yOU使用<代码> .= /COD>连接所有字符串。最后,返回整个字符串。请注意,用最后的<代码> Outs<代码>创建一个没有任何动作的空窗体。请考虑是否有必要在该示例中创建表单。是否忘记了“关闭<代码>”
第2行?有机会的话,我会尝试一下你的解决方案。好吧,这不仅仅是混乱,因为如果你添加第三个变量,那就更困难了。我喜欢你简化的想法,但如果我添加第三个变量,甚至第四个变量,它将开始变得很长。