簡體   English   中英

多條件php if語句帶有變量的最佳實踐是什么?

[英]What is the best practice for a multi-conditional php if statements with variables?

我有一個獲取內部變量的形式。 原始看起來像這樣:

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;

你應該開始產生,而不是產生一個內一切的代碼一步一步, 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;

由於主要的抱怨是混亂 ,為減少混亂 ,可以將on | off參數的值連接起來 ,然后使用switch 像這樣:

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

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM