How can PHP code be prevented from executing immediately upon page load and only run after a submit button is clicked?

To prevent PHP code from executing immediately upon page load and only run after a submit button is clicked, you can use a conditional check to see if the form has been submitted. This can be achieved by checking if the request method is POST and if a specific form field has been set. If these conditions are met, then the PHP code will execute.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit"])) {
    // PHP code to be executed after the submit button is clicked
}
?>