检查是否所有 $_POST 字段都用 php 填充的快捷方式


Shortcut for checking if all $_POST fields are filled in php

我知道我可以_POST通过使用

empty / isset

但是,我在这里有很多领域。是否有任何快捷方式可以检查是否填写了所有字段?而不是做

if (!empty($_POST['a']) || !empty($_POST['b']) || !empty($_POST['c']) || !empty($_POST['d']).... ad nauseum)

提前感谢!

您可以使用array_filter并比较两个计数

if(count(array_filter($_POST))!=count($_POST)){
    echo "Something is empty";
}

您可以遍历 $_POST 变量。

例如:

$messages=array();
foreach($_POST as $key => $value){
    if(empty($value))
        $messages[] = "Hey you forgot to fill this field: $key";
} 
print_r($messages);

这是我刚刚编写的一个函数,可能会有所帮助。

如果您传递的任何参数为空,则返回 false。 如果不是,它将返回 true。

function multi_empty() {
    foreach(func_get_args() as $value) {
        if (!isset($value) || empty($value)) return false;
    }
    return true;
}

multi_empty("hello","world",1234); //Returns true 
multi_empty("hello","world",'',1234); //Returns false
multi_empty("hello","world",1234,$notset,"test","any amount of arguments"); //Returns false 
您可以使用

foreach()循环来检查每个$_POST值:

foreach ($_POST as $val) {
    if(empty($val)) echo 'You have not filled up all the inputs';
}