It is very essential to have the input to your form validated before taking the form submission data for further processing.
When there are many fields in the form, the PHP validation script becomes too complex.
We will cover a few samples like validating e-mail address and URLs later. Here we will name it check_input and simply call this function whenever we need to validate simple input data: Note the check_input function at the bottom.
What it does is takes the data passed to the function, strips unwanted characters (extra space, tab, newline) from the beginning and end of the data using the PHP trim() function, strips any quotes escaped with slashes and passes it through htmlspecialchars().
Moreover, since you are doing the same or similar validation for most of the forms that you make, just too much of duplicate effort is spent on form validations.