PHP can run from the command line to automate tasks and run cron jobs. Use $argv for arguments and echo for output. The Symfony Console component makes building feature-rich CLI tools straightforward.
PHP on the Command Line
#!/usr/bin/env php
<?php
// Command line arguments
$script = $argv[0]; // script name
$first = $argv[1]; // first argument
$all = $argc; // total argument count
// Interactive input
echo "Enter your name: ";
$name = trim(fgets(STDIN));
echo "Hello, $name!\n";
// Colour output
echo "\033[32mSuccess!\033[0m\n"; // green
echo "\033[31mError!\033[0m\n"; // red
// Progress output
for ($i = 0; $i <= 100; $i += 10) {
echo "\r[$i%] Processing...";
sleep(1);
}
echo "\nDone!\n";
// Exit codes
exit(0); // success
exit(1); // failure
php script.php arg1 arg2
php -r "echo phpinfo();"
php -l script.php // syntax check