What are some best practices for optimizing the performance of a PHP script that fetches emails from a POP3 account and processes them, considering factors like script execution time and memory usage?

To optimize the performance of a PHP script that fetches emails from a POP3 account and processes them, it is important to consider factors like script execution time and memory usage. Some best practices include using efficient PHP functions for handling email operations, minimizing unnecessary loops and conditionals, utilizing caching mechanisms for repeated operations, and optimizing database queries if data storage is involved.

// Example PHP code snippet for optimizing the performance of fetching emails from a POP3 account

// Connect to the POP3 server
$hostname = 'pop.example.com';
$username = 'username';
$password = 'password';
$inbox = imap_open('{' . $hostname . ':110/pop3}', $username, $password);

// Check for new emails
$emails = imap_search($inbox, 'ALL');

// Process each email
if ($emails) {
    foreach ($emails as $email_number) {
        $header = imap_headerinfo($inbox, $email_number);
        $from = $header->fromaddress;
        $subject = $header->subject;
        
        // Process email content
        
        // Mark email as read
        imap_setflag_full($inbox, $email_number, "\\Seen");
    }
}

// Close the connection
imap_close($inbox);