What are best practices for optimizing CPU usage in PHP scripts that run indefinitely?

When running PHP scripts indefinitely, it's important to optimize CPU usage to prevent excessive resource consumption. One effective way to achieve this is by implementing a sleep function within the script to introduce delays between iterations, reducing the frequency of CPU usage.

while (true) {
    // Your script logic here

    // Introduce a delay to reduce CPU usage
    sleep(1);
}