Skip to content

Instantly share code, notes, and snippets.

@trq
Last active August 29, 2015 14:02
Show Gist options
  • Save trq/f134fe5e1f9631c41f59 to your computer and use it in GitHub Desktop.
Save trq/f134fe5e1f9631c41f59 to your computer and use it in GitHub Desktop.
<?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";
}
}
<?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