How can PHP be used to ensure that a specific function is only executed once during the initial page load?

To ensure that a specific function is only executed once during the initial page load, you can use a static variable within the function to keep track of whether it has already been executed. By checking this variable before executing the function, you can prevent it from running multiple times.

function myFunction() {
    static $executed = false;
    
    if (!$executed) {
        // Code to execute only once during initial page load
        $executed = true;
    }
}

// Call the function to execute it
myFunction();