What is the correct way to check if all form fields are filled in PHP?

When working with forms in PHP, it is important to ensure that all required fields are filled out before processing the form data. One way to check if all form fields are filled is to iterate through each field and check if it is empty. If any field is empty, you can display an error message to prompt the user to fill out all required fields.

// Check if all form fields are filled
$required_fields = ['field1', 'field2', 'field3']; // List of required fields

foreach($required_fields as $field) {
    if(empty($_POST[$field])) {
        // Display error message and stop form submission
        echo "Please fill out all required fields.";
        exit;
    }
}

// All required fields are filled, proceed with form processing
// Your form processing code here