Creating a PHP CLI App
January 22, 2025PHP is primarily known for web development, but it’s also an excellent choice for building command-line interface (CLI) applications. With a few tools and techniques, you can create powerful CLI apps using PHP.
I’ll walk you through creating a simple PHP CLI application, from setup to execution.
Prerequisites
Before we begin, ensure you have the following:
PHP Installed: Verify PHP is installed on your system by running:
php -v
If PHP isn’t installed, download and install it from php.net.
Basic Command-Line Knowledge: Familiarity with terminal commands will be helpful.
Set Up Your Project
Start by creating a directory for your CLI app. For example:
mkdir my-php-cli-app
cd my-php-cli-app
Then, initialize a Composer project:
composer init
Follow the prompts to set up your project metadata.
Create the Entry Point
In PHP CLI apps, the entry point is usually a single script file. Create a file named app.php
:
touch app.php
Add the following code to app.php
:
#!/usr/bin/env php
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
// Main application logic
echo "Welcome to My PHP CLI App!\n";
Make the file executable:
chmod +x app.php
Parse Command-Line Arguments
Use PHP’s getopt()
function to handle arguments. Update app.php
as follows:
#!/usr/bin/env php
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
$options = getopt("n:hv", ["name:", "help", "version"]);
if (isset($options['h']) || isset($options['help'])) {
echo "Usage: app.php [options]\n";
echo "Options:\n";
echo " -n, --name Set your name\n";
echo " -h, --help Display this help message\n";
echo " -v, --version Show application version\n";
exit(0);
}
if (isset($options['v']) || isset($options['version'])) {
echo "My PHP CLI App Version 1.0\n";
exit(0);
}
$name = $options['n'] ?? $options['name'] ?? 'World';
echo "Hello, $name!\n";
Now, you can pass arguments like:
php app.php -n Kostas
# Output: Hello, Kostas!
Use a CLI Framework (Optional)
For more complex applications, consider using a PHP CLI framework like Symfony Console:
-
Install Symfony Console:
composer require symfony/console
-
Update
app.php
:#!/usr/bin/env php <?php require_once __DIR__ . '/vendor/autoload.php'; use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class GreetCommand extends Command { protected static \$defaultName = 'greet'; protected function configure() { \$this ->setDescription('Greets the user') ->addArgument('name', null, 'The name of the user', 'World'); } protected function execute(InputInterface \$input, OutputInterface \$output): int { \$name = \$input->getArgument('name'); \$output->writeln("Hello, \$name!"); return Command::SUCCESS; } } \$application = new Application(); \$application->add(new GreetCommand()); \$application->run();
-
Run your command:
php app.php greet Kostas # Output: Hello, Kostas!
Distribute Your Application
Package your CLI app as a PHAR (PHP Archive) for easy distribution:
-
Create a
box.json
configuration file:{ "directories": ["src"], "main": "app.php" }
-
Install Box:
composer require humbug/box --dev
-
Build the PHAR:
vendor/bin/box compile
Your app is now bundled into a single executable PHAR file.
Congratulations! You’ve created a PHP CLI application from scratch. Whether you’re writing simple scripts or complex tools, PHP provides the flexibility and tools to make it happen. Try expanding your app with additional commands, error handling, or third-party libraries to make it even more powerful.