如何在PHP中缩短多个isset函数

时间:2014-05-26 06:22:43

标签: php if-statement isset

看看我的剧本:

if((isset($_POST['unma']) && isset($_POST['livi'])) && (isset($_POST['cont']) && 

    isset($_POST['phone']))){
        if(
            (
                (
                    (
                        (isset($_POST['fnma']) && isset($_POST['lnma']))
                        &&
                        (isset($_POST['occ']) && isset($_POST['hom']))
                    )
                    &&
                    (
                        (isset($_POST['town']) && isset($_POST['dist']))
                        &&
                        (isset($_POST['dispmb']) && isset($_POST['fbc']))
                    )
                )
                &&
                (
                    (
                        (isset($_POST['gp']) && isset($_POST['twt']))
                        &&
                        (isset($_POST['ins']) && isset($_POST['flc']))
                    )
                    &&
                    (
                        (isset($_POST['ile']) && isset($_POST['cam']))
                        &&
                        (isset($_POST['cam_co']) && isset($_POST['prof_phr']))
                    )
                )
            )
            &&
            isset($_POST['awards'])
        ){
                // Codes here
        }

如何缩短此isset个功能。我找到了foreach的解决方案,但他们没有检查帖子isset,而是检查发布的值是否为空

5 个答案:

答案 0 :(得分:2)

您可以使用

if (isset($_POST['a'], $_POST['b'], $_POST['c'], ...))

等等。如果未设置任何变量,那么您将获得false

答案 1 :(得分:0)

$expectedKeys = ['fnma', 'lnma', ...];
if (!array_diff_key(array_flip($expectedKeys), $_POST)) {
    // all keys are set
}

答案 2 :(得分:0)

我认为这样的事情会起作用:

 // add all names of the values that needs to be set in the `$_POST` array
 $values = array('fnma', 'lnma');

 function checkSet($array) {
   foreach($array as $value) {
     if(!isset($_POST[$value])) return false;
   }
   return true;
 }
 if(checkSet($values)) {
   // your code
 }

答案 3 :(得分:0)

将必填字段放在数组中并使用循环检查它,可以轻松轻松地添加必填字段。

$req_values = array('fnma','lnma','occ','hom','town','dist','dispmb','fbc','gp','twt','ins','flc','ile','cam','cam_co','prof_phr');


foreach($req_values as $key) {
  if (empty($_POST[$key])) {
           echo $key .'is required';
           exit;
      }
}

答案 4 :(得分:0)

知道isset可以采用多个值,您可以这样做:

if (isset($_POST['unma'], $_POST['livi'], $_POST['cont'], $_POST['phone'], $_POST['fnma'], $_POST['lnma'], $_POST['occ'], $_POST['hom'], $_POST['town'], $_POST['dist'], $_POST['dispmb'], $_POST['fbc'], $_POST['gp'], $_POST['tat'], $_POST['ins'], $_POST['flc'], $_POST['ile'], $_POST['cam'], $_POST['cam_co'], $_POST['prof_phr'], $_POST['awards'])) {
    // Codes here
}

但这似乎是噩梦。我会将$_POST键设为数组&然后执行以下操作:

// Set an array of post values.
$post_values = array('unma','livi','cont','phone','fnma','lnma','occ','hom','town','dist','dispmb','fbc','gp','tat','ins','flc','ile','cam','cam_co','prof_phr','awards');

// Set '$post_valid' to an array.
$post_valid = array();

// Roll through the post values.
foreach ($post_values AS $post_key => $post_value) {
  // If the value is  set, set '$post_valid' to TRUE or else set it to FALSE.
  // $post_valid = isset($_POST[$post_key]) ? TRUE : FALSE; // Not really needed. Simple `isset` should work.
  $post_valid[$post_key] = !empty($_POST[$post_key]);
}

// Now if '$post_valid' values are all true, do something.
$post_valid_values = array_values($post_valid);
if (!empty($post_valid_values) && (count($post_valid_values) == count($post_values))){
  // Codes here
}