What are the best practices for structuring PHP code to ensure proper execution and output, especially in scenarios involving form submissions?

When handling form submissions in PHP, it is essential to properly structure the code to ensure proper execution and output. One best practice is to separate the form processing logic from the presentation logic by using a conditional check to determine if the form has been submitted. This helps in organizing the code and handling form submissions effectively.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    
    // Validate form data
    // Perform necessary actions
    
    // Redirect or display success message
    header("Location: success.php");
    exit();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Form Submission</title>
</head>
<body>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <input type="text" name="name" placeholder="Name">
        <input type="email" name="email" placeholder="Email">
        <button type="submit">Submit</button>
    </form>
</body>
</html>