Automating Routine with PHP: 5 Powerful Scripts That Will Save You Hours of Work

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Your Personal Assistant in PHP: Why Automation is a Superpower



Imagine a Monday morning. You come to the office, open your laptop, and all the routine is already done: backups are uploaded to the server, reminders have been sent to clients, and yesterday's CSV reports are already processed and neatly packaged into PDFs. Sounds like science fiction? Well, it's not.



Most PHP developers (and not only them) spend up to 30% of their working time on monotonous, repetitive tasks. Automation with PHP is not magic, but the smart use of built-in functions and a couple of libraries. Today, I'll show you 5 real scripts that you can run within 10 minutes of reading this article.



By the way, if you're interested in automation with Python, we have great materials on PythonLib. But today we're talking about PHP, because it still rules the server side (up to 77% of websites run on it).



1. Automatic Cleanup and Archiving of Old Logs



Logs grow like a snowball. Yesterday the error.log file was 50 MB, and today it's already 500. Manual cleanup is a pain. Let's write a PHP script for automation of this task.



Script: LogRotator



<?phpclass LogRotator {    private string $logDir;    private string $archiveDir;    private int $maxFileAgeDays;

public function __construct(string $logDir, string $archiveDir, int $maxFileAgeDays = 30) { $this->logDir = rtrim($logDir, '/'); $this->archiveDir = rtrim($archiveDir, '/'); $this->maxFileAgeDays = $maxFileAgeDays; }

public function rotate(): void { if (!is_dir($this->archiveDir)) { mkdir($this->archiveDir, 0755, true); }

$files = glob($this->logDir . '/*.log'); foreach ($files as $file) { $fileAge = time() - filemtime($file); if ($fileAge > $this->maxFileAgeDays * 86400) { $archiveName = $this->archiveDir . '/' . basename($file) . '.' . date('Ymd') . '.gz'; $gzData = gzencode(file_get_contents($file), 9); file_put_contents($archiveName, $gzData); unlink($file); echo "Archived: " . basename($file) . PHP_EOL; } } }}

// Usage:$rotator = new LogRotator('/var/log/myapp', '/backup/logs/archive');$rotator->rotate();?>


This PHP automation script does three things: finds files older than 30 days, compresses them into GZIP, and moves them to an archive. Run it via CRON once a week — and forget about "disk full".



2. Mass Sending of Personalized Email Notifications



Let's say you have an online store. A customer places an order, and they need to receive an email with the details. Doing this manually is foolish. Automating tasks with PHP using the built-in mail() function or PHPMailer will solve the problem.



Example: Sending Abandoned Cart Reminders



<?phpfunction sendAbandonedCartReminder(string $email, string $customerName, array $items): bool {    $subject = "You forgot your cart, $customerName! 🛒";        $message = "<h2>Hello, $customerName!</h2>";    $message .= "<p>You left these items:</p><ul>";        foreach ($items as $item) {        $message .= "<li>{$item['name']} - {$item['price']} RUB</li>";    }        $message .= "</ul><p><a href='https://yourshop.com/cart'>Return to cart</a></p>";        $headers = "MIME-Version: 1.0\\r";    $headers .= "Content-Type: text/html; charset=UTF-8\\r";    $headers .= "From: shop@yourshop.com\\r";        return mail($email, $subject, $message, $headers);}

// Mass mailing:$customers = [ ['email' => 'ivan@example.com', 'name' => 'I

Blogs

Book Recommendations