The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Try out my first PHP web app! Reply To: Try out my first PHP web app!


That’s a good approach. You need to pass $required into the function (vars outside are not available inside). Also, once you’re done, you need to return true (otherwise, it would be difficult to tell whether everything passed the test or not).

function validate_required( $required,$input ){
    foreach( $required as $name ){
        if( empty( $input[$name] ) ){ return false; }
        return true;


$required = ['name','email'];
$allGood = validate_required( $required,$_POST );
if( $allGood ){ /*  good job!  */ }
else{ /*  you forgot some.  */ }

You might also be interested in examining how we implemented form validation here.