
PHP Command Line Interface (CLI): Practical Examples and Best Practices
Getting Started with PHP CLI
To use PHP CLI, ensure you have PHP installed on your system. You can check your installation by running the following command in your terminal:
php -vThis command should return the version of PHP installed on your machine. If you see an error, you may need to install PHP or add it to your system's PATH.
Basic PHP CLI Script
A simple PHP CLI script can be created using any text editor. Below is an example of a basic script that outputs "Hello, World!" to the terminal.
<?php
// hello.php
echo "Hello, World!\n";
?>To execute this script, navigate to the directory where the file is located and run:
php hello.phpAccepting Command-Line Arguments
PHP CLI allows for the acceptance of command-line arguments, which can be accessed via the $argv array. Here’s an example script that takes a name as an argument and greets the user.
<?php
// greet.php
if ($argc > 1) {
$name = $argv[1];
echo "Hello, $name!\n";
} else {
echo "Usage: php greet.php [name]\n";
}
?>To run this script, use:
php greet.php JohnThis will output:
Hello, John!Using PHP CLI for File Operations
PHP CLI can also be used for file operations. Below is an example of a script that reads a file and counts the number of lines.
<?php
// count_lines.php
if ($argc < 2) {
echo "Usage: php count_lines.php [filename]\n";
exit(1);
}
$filename = $argv[1];
if (!file_exists($filename)) {
echo "File not found: $filename\n";
exit(1);
}
$lines = file($filename);
echo "Number of lines in $filename: " . count($lines) . "\n";
?>To use this script, create a text file (e.g., sample.txt) and run:
php count_lines.php sample.txtCreating a Simple PHP CLI Application
You can create a more complex CLI application by organizing your code into functions and using a switch-case structure for command handling. Below is an example of a simple calculator.
<?php
// calculator.php
function add($a, $b) {
return $a + $b;
}
function subtract($a, $b) {
return $a - $b;
}
function multiply($a, $b) {
return $a * $b;
}
function divide($a, $b) {
if ($b == 0) {
return "Cannot divide by zero.";
}
return $a / $b;
}
if ($argc < 4) {
echo "Usage: php calculator.php [add|subtract|multiply|divide] [num1] [num2]\n";
exit(1);
}
$operation = $argv[1];
$num1 = $argv[2];
$num2 = $argv[3];
switch ($operation) {
case 'add':
echo add($num1, $num2) . "\n";
break;
case 'subtract':
echo subtract($num1, $num2) . "\n";
break;
case 'multiply':
echo multiply($num1, $num2) . "\n";
break;
case 'divide':
echo divide($num1, $num2) . "\n";
break;
default:
echo "Unknown operation: $operation\n";
break;
}
?>You can run this calculator by executing commands like:
php calculator.php add 5 3Best Practices for PHP CLI Scripts
- Use PHP's Built-in Functions: Leverage PHP's extensive built-in functions to simplify your scripts.
- Error Handling: Implement error handling to manage unexpected input or runtime errors.
- Documentation: Comment your code and provide usage instructions to enhance readability and maintainability.
- Input Validation: Always validate input to ensure your script behaves as expected and to prevent security vulnerabilities.
- Script Structure: Organize your code into functions or classes to promote reusability and clarity.
Summary of PHP CLI Features
| Feature | Description |
|---|---|
| Command-Line Arguments | Accessed via $argv and $argc arrays |
| File Operations | Read, write, and manipulate files easily |
| Built-in Functions | Utilize PHP's extensive library of functions |
| Error Handling | Implement robust error handling mechanisms |
