CLI utilities in PHP: why haven't you written your console tool yet?
\n\nImagine: you're sitting in the terminal, run one command — and in a second you have a ready report, all images are converted, or a project is set up on a new server. No browser, no mouse, no unnecessary movements. Sounds like magic? No, these are ordinary CLI utilities in PHP.
\n\nMany developers consider PHP only a "web language". But in fact, PHP is an excellent tool for creating console applications. It has built-in work with input-output streams, convenient argument handling, and, of course, powerful libraries like Symfony Console. In this article, we'll break down how to create a console script in PHP from scratch — from a simple echo to a full-fledged tool with colors and a progress bar.
\n\nIf you're tired of doing repetitive tasks manually, if you want to automate routine and feel like a real hacker — you've come to the right place. Let's go!
\n\nWhat is a CLI utility and why do you need it?
\n\nCLI (Command Line Interface) is a program that works in the command line. It accepts arguments, performs actions, and outputs the result. Unlike web applications, there is no HTML, CSS, or JavaScript here. Only pure code and the terminal.
\n\nWhy write CLI utilities in PHP? Here are a few scenarios:
\n\n- \n
- Data migrations — transferring information from CSV to a database. \n
- Report generation — collecting statistics and generating PDF or Excel. \n
- Deployment automation — deploying a project on a server with one command. \n
- Parsing and file processing — conversion, compression, renaming. \n
- CRON tasks — regular operations that run on a schedule. \n
And most importantly — developing console tools in PHP does not require complex infrastructure. One file and a few lines of code are enough.
\n\nGetting started: a minimal CLI script in PHP
\n\nOpen your terminal, create a file hello.php and write:
<?php\necho "Hello, terminal!\\n";\n\n\nRun it:
\n\nphp hello.php\n\n\nYou will see:
\n\nHello, terminal!\n\nCongratulations! You've just written your first CLI utility in PHP. But let's make it useful. Let's add command line argument handling.
\n\nWorking with arguments: $argv and $argc
\n\nPHP provides two superglobal arrays for working with the console: $argv (list of arguments) and $argc (number of arguments). The first argument (index 0) is the name of the script itself.
Example:
\n\n<?php\nif ($argc < 2) {\n echo "Usage: php hello.php [name]\\n";\n exit(1);\n}\n\n$name = $argv[1];\necho "Hello, $name!\\n";\n\n\nRun:
\n\nphp hello.php Alexey\n\n\nResult:
\n\nHello, Alexey!\n\nAlready interesting. But for serious projects, this approach is inconvenient. You need named arguments, flags, default values. This is where the Symfony Console library comes to the rescue.
\n\nSymfony Console: a powerful foundation for CLI utilities in PHP
\n\nSymfony Console is the de facto standard for creating console applications in PHP. With it, you can easily add:
\n\n- \n
- Named arguments and options \n
- Colored output \n
- Progress bars \n
- Input validation \n
- Command autoloading \n
Installation via Composer:
\n\ncomposer require symfony/console\n\n\nNow let's create a full-fledged CLI utility in PHP — a password generator.
\n\nWriting a command for password generation
\n\nProject structure