Skip to content

Instantly share code, notes, and snippets.

@assertchris
Last active March 28, 2019 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save assertchris/d6b7da858fbfb9a85bbf4ef3b1654c0d to your computer and use it in GitHub Desktop.
Save assertchris/d6b7da858fbfb9a85bbf4ef3b1654c0d to your computer and use it in GitHub Desktop.
<?php
$expected = ["name", "email", "comments"];
$required = ["name", "comments"];
foreach ($_POST as $key => $value) {
if (!is_array($value)) {
$value = trim($value);
}
if (!in_array($key, $expected)) {
// ignore the value, it's not in $expected
continue;
}
if (!in_array($key, $required)) {
// optional value, which we'll default to empty string
$$key = $value ?? "";
continue;
}
if (empty($value)) {
// required value is missing
$missing[] = $key;
$$key = "";
continue;
}
$$key = $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment