I just stumbled across this fantastic snippet of code to check for the existence of multiple variables with the global $_POST.

Previously I would have check the variables by stringing together multiple isset functions like so.

if (isset($_POST['key1']) && isset($_POST['key2']) && isset($_POST['key3'])) { //do something here }

However, the new snippet I recently found is great for accomplishing this check in a much more efficient way.

$check_post = array('key1', 'key2', 'key3');
if (!array_diff($check_post, array_keys($_POST))) {
     //do something here
}

This will really make checking for the existence of POST variables much easier.

Cheers!

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *