PHP is Not Just for the Web: Why Your Next CLI Tool Should Be Written in PHP
When we hear "PHP," the first association is websites, WordPress, and endless echo statements in the browser. But PHP is a true Swiss Army knife for developers. One of its most underestimated features is the ability to create full-fledged CLI utilities in PHP. Yes, those console programs that help automate routine tasks, parse logs, generate reports, and make a DevOps engineer's life a little easier.
In this article, we won't just talk theory. We'll actually write several real console applications in PHP from scratch. You'll learn how to handle command-line arguments, colorize output, display progress bars, and even create interactive menus. By the end of the article, you'll have a ready-made template you can use immediately in your projects.
Why PHP for CLI? 3 Compelling Arguments
Before diving into the code, let's figure out why you'd use PHP for creating CLI utilities in the first place. After all, there's Python, Node.js, Go…
- You already know PHP. If you're a PHP web developer, you don't need to learn a new syntax to write a script for database backup or deployment.
- Unified ecosystem. You can use the same libraries (via Composer), the same ORMs, and the same template engines. Want a script that generates a PDF report from a database? No problem.
- Development speed. PHP scripts are written very quickly. For simple utilities, this is an ideal option — no need to compile, configure the environment, just run and go.
Of course, for high-load daemons or complex system utilities, it's better to use C or Rust. But for 90% of everyday tasks, PHP is perfectly suitable.
From Scratch to Your First CLI Utility: Hello, World! in the Terminal
Let's start with the simplest thing. Create a file hello.php and write:
#!/usr/bin/env php<?php
echo "Hello, terminal!";The first line is a shebang. It tells the system that this file should be executed with PHP. Without it, you'd have to write php hello.php every time; with it, you can just write ./hello.php.
Don't forget to make the file executable:
chmod +x hello.phpNow run it:
./hello.phpCongratulations! You've just created your first CLI utility in PHP. It's incredibly simple, right?
Working with Arguments: Making the Utility Useful
Any serious console program must be able to accept arguments. PHP provides two superglobal arrays for this: $argv and $argc.
$argv— an array of arguments. The first element is the script name.$argc— the number of arguments.
Let's write a simple calculator — add two numbers passed as arguments.
Simple PHP CLI Calculator
#!/usr/bin/env php<?php
if ($argc !== 3) { echo "Usage: {$argv[0]} <number1> <number2>"; exit(1);}
$a = (int)$argv[1];$b = (int)$argv[2];
echo "Sum: " . ($a + $b) . "";echo "Difference: " . ($a - $b) . "";echo "Product: " . ($a * $b) . "";echo "Quotient: " . ($a / $b) . "";Run it:
./calc.php 10 5Output:
Sum: 15Difference: 5Product: 50Quotient: 2But this is too primitive. Real projects need named arguments (flags), like in git commit -m "message". For this, PHP has the getopt() function.
Named Arguments with getopt()
#!/usr/bin/env php<?php
$shortOptions = "u