[英]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。 如果不是它将返回真实。
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';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.