Lots of frameworks do a great job handling this task for you, and you should take a look at Symfony Forms.
That being said, one of the most common tasks of a Web Developer will usually be working with Forms. You’ll create hundreds of forms and in turn, write hundreds of pieces of code to handle some type of user submitted data.
Early on this became the worse part of my job. So I started to use a quick way to handle post data. What’s not shown here is the very very very important step of validating the user input.
Two pieces of code are at work here. The
foreach() will iterate over the values of the form, while the
switch case let’s us use a boolean flag to determine if the data is correct. Unfortunately I still come across lengthy
if else/else if/else in some PHP code out in the wild.
The only real missing piece from this shortcut is ensuring all of the
$_POST values are present. This could be done by comparing all of the values in your
$clean array are present in