简体   繁体   English

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

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

I know that I can check if the superglobal $_POST is empty or not by using 我知道我可以通过使用来检查超全局$ _POST是否为空

empty / isset

However, I have many fields here. 但是,我这里有很多领域。 Is there any shortcut to check if all fields are filled? 是否有任何快捷方式可以检查是否所有字段都已填满? Instead of doing 而不是做

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

Thanks in advance! 提前致谢!

You can use array_filter and compare both counts 您可以使用array_filter并比较两个计数

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

You can loop through the $_POST variable. 你可以遍历$ _POST变量。

For example: 例如:

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

Here's a function I just authored that might help. 这是我刚刚撰写的一个可能有用的功能。

if any of the arguments you pass is empty, it returns false. 如果您传递的任何参数为空,则返回false。 if not it'll return true. 如果不是它将返回真实。

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

Example

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 

You can use a foreach() loop to check each $_POST value: 您可以使用foreach()循环来检查每个$_POST值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM