How can conditional statements in PHP be used to control the behavior of form submissions and prevent unintended actions like automatic sending?

To control the behavior of form submissions and prevent unintended actions like automatic sending, you can use conditional statements in PHP to check for specific conditions before processing the form data. For example, you can check if certain form fields are empty or if the form was submitted by a human user rather than a bot.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (!empty($_POST['name']) && !empty($_POST['email'])) {
        // Process the form data
        // Send email, save to database, etc.
    } else {
        // Display an error message or redirect back to the form
        echo "Please fill out all required fields.";
    }
}
?>