开发者

simplify/reduce code - if

$form = array();
$form = $_POST['data'];

    function livre($form) {
        if (empty($form["radios"]) || empty($form["age"]) 
        || empty($form["gender"]) || empty($form["civil"]) 
        || empty($form["formation_area"]) || empty($form["scholarithy"])
        || empty($form["professional_activity"]) || empty($form["city_work"])
        || empty($form["contract_job"]) || empty($form["salary"])){
            echo ("empty");
        }
        else
        echo ("not empty");

    }

        livre($form_data_array);

the first question is: Is possible reduce the number of empty to one, like empty($form) || ($form1)

the second is: how i can reduce the number of lines, i feel this scheme that i use is not the best way. I ca开发者_如何学JAVAn't simple verify if the array is empty because only some indexes can be empty.

thanks


You can store all the field names that have to be tested in an array:

$fields = array('radios', 'age', ...);

and then loop over it:

foreach($fields as $field) {
   if(empty($form[$field])) {
       echo 'empty';
       break;
   }
}

Side note: You don't need $form = array();.


A more compact method would be:

if (count(array_filter($form, "strlen")) != count($form)) {
    echo "one field was empty";
}

Note that it does an actual strlen test instead of empty. That's more appropriate for text fields, because it doesn't treat "0" as absent.


$empty = false;
foreach ($_POST as $value) 
{ 
   if (empty($value))
       $empty=true;
}


You can search for each element in the form in a loop:

function livre($form)
{
    $elements = array(
      'radios',
      'age',
      ...
    );

    foreach($elements as $el)
    {
      if(!key_exists($el, $form)
      {
        echo 'empty';
      }
    }
    echo 'not empty';
}
livre($form_data_array);


while ($form[$i])
{
if (empty($form[$i])
   echo "empty";
else
   $i++;
}


You could try to do like this instead.

function livre($form) {
        if(is_valid($form))
            echo("valid")
        else
            echo ("not valid");

}
function is_valid($post) {
    $required_fields = ["radios", "age", "gender", "civil", "formation_area", "scholarithy", 
                       "professional_activity", "city_work", "contract_job", "contract_job", "salary"]

    for($key in $required_fields)
        if(empty($post[$key]))
            return false;
    return true;
}

I introduced a seperate function to validate the post data and I did a for-loop to check if the data is valid or not.


I like the answer given by Mario a lot!!

If this works this could be a bit fast:

    function livre() 
    {
    $form = $_POST['data'];

        if (
    $form["radios"]==''|| $form["age"]=='' || $form["gender"]==''||
    $form["civil"]=='' || $form["formation_area"]=='' || 
    $form["scholarithy"] =='' || $form["professional_activity"]==''||
    $form["city_work"]=='' || $form["contract_job"]=='' ||
    $form["salary"]==''
    )
      echo ("empty");

        else
        echo ("not empty");

    }

livre(); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜