So I have this:
$title = $_POST['title'];
$keywords = $_POST['keywords'];
$purpose = $_POST['purpose'];
$special = $_POST['special'];
$length = $_POST['length'];
$email = $_POST['email'];
I want to run a statement if any of these variables is empty. I know I can run an if statement checking if any of them are empty but is there a simpler way or perhaps a function that could do it with less code?
I could do this:
if($title == '' || $keywords == '' || etc etc...
But is there a method to do it with less code? That just seems a bit clunky.
Thanks!
$vars=array('title','keywords','purpose','special','length','email');
$allfilled=true;
foreach ($vars as $var) {
$$var=$_POST[$var];
if ($$var == '') $allfilled=false;
}
if (!$allfilled) ...
For fix it i usually use this solution.
$keys = array('title','keywords','any_other_key');
$isError = false;
$data = array();
foreach($keys as $key) {
if (!isset($_POST[$key]) || $_POST[$key] == '') {
$isError = $key;
break;
} else {
$data[$key] = strip_tags($_POST[$key]);
}
}
if ($isError !== false) {
// Error. Some fields are not filled!
// Error field are in $isError variable;
}