I usually use a function to validate my post and it is an answer for this question too so let me post it.
to call my function I will use the 2 array like this
validatePost(['username', 'password', 'any other field'], $_POST))
then my function will look like this
function validatePost($requiredFields, $post) { $validation = []; foreach($requiredFields as $required => $key) { if(!array_key_exists($key, $post)) { $validation['required'][] = $key; } } return $validation; }
this will output this
"required": ["username","password","any other field" ]
so what this function does is validate and return all the missing fields of the post request.