CLI utilities in PHP: creating console tools from scratch

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

CLI utilities in PHP: why haven't you written your console tool yet?

\n\n

Imagine: 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\n

Many 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\n

If 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\n

What is a CLI utility and why do you need it?

\n\n

CLI (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\n

Why 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
\n\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\n

Getting started: a minimal CLI script in PHP

\n\n

Open your terminal, create a file hello.php and write:

\n\n
<?php\necho "Hello, terminal!\\n";\n
\n\n

Run it:

\n\n
php hello.php\n
\n\n

You will see:

\n\n
Hello, terminal!
\n\n

Congratulations! You've just written your first CLI utility in PHP. But let's make it useful. Let's add command line argument handling.

\n\n

Working with arguments: $argv and $argc

\n\n

PHP 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.

\n\n

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\n

Run:

\n\n
php hello.php Alexey\n
\n\n

Result:

\n\n
Hello, Alexey!
\n\n

Already 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\n

Symfony Console: a powerful foundation for CLI utilities in PHP

\n\n

Symfony 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
\n\n

Installation via Composer:

\n\n
composer require symfony/console\n
\n\n

Now let's create a full-fledged CLI utility in PHP — a password generator.

\n\n

Writing a command for password generation

\n\n

Project structure

Blogs

Book Recommendations