Last active
August 29, 2015 14:02
-
-
Save trq/f134fe5e1f9631c41f59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$errors = array(); | |
foreach (array('email', 'name', 'age', 'occupation') as $key) { | |
if (!isset($_GET[$key]) || empty($_GET[$key])) { | |
$errors[] = "You need to fill in $key"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function validateArray(array $keys, array $subject) | |
{ | |
$errors = array(); | |
foreach ($keys as $key) { | |
if (!isset($subject[$key]) || empty($subject[$key])) { | |
$errors[] = $key; | |
} | |
} | |
return $errors; | |
} | |
// Now you can do. | |
foreach (validateArray(array('email', 'name', 'age', 'occupation'), $_GET) as $key) { | |
echo "You need to fill in $key"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment