You can also write:
<?php if (isset($_POST) && $_POST != NULL ){ foreach ($_POST as $key => $value) {
Here is a more detailed example:
<?php if (isset($_POST) && $_POST != NULL) { $clean = array(); foreach ($_POST as $key => $value) { switch($key) { case "a": if (ctype_digit($value)){ $clean[$key] = $value; } break; case "b": if ( ctype_alpha($value)){ $clean[$key] = $value; } break; case "c": if ( ctype_alnum($value)){ $clean[$key] = $value; } break; case "d": if (ctype_punct($value)){ $clean[$key] = $value; } break; default: echo $value, " is invalid data\n"; } } var_dump($clean); } ?>
So, if you had a form with input fields a, b, c, d and someone forged your form and added the e field, then the previous code did not accept the value from the e field.
source share