[英]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.