Last active
March 28, 2019 14:28
-
-
Save assertchris/d6b7da858fbfb9a85bbf4ef3b1654c0d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
$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