PHP command Line
Here is a way to run your PHP from command line. This example is
working in Linux.
Multi-line comment is added as help. You can remove
the multi-line comments and edit as you like for your future reference.
On window if PHP is included in your PATH , then you will be able to run all the commands. File will be used from “C:\” if you do not specify file path. In Linux file’s default location will be your home directory.
PHP command line: You may need to do something in php.ini
output_buffering=off /I In interactive mode buffering will stop output until buffer fill
implicit_flush=on // immediate output
Why to use PHP CLI? One wants to use PHP CLI SAPI simply because there are several advantages in being able to run PHP code from command line such as:
- no need to learn another language such as Perl, Bash or Awk
- running scheduled (CRON) tasks written in php
- making GUI applications with PHP and GTK
- reusage of your existing components
- write very robust scripts for your system by using PHP5 multithreadingi capabilities
- access system STDIN, STDOUT, STERR with PHP
Command Line Interface and CRON : Runing PHP scripts in CRON allows you to do scheduled tasks. For example, check weather links on your links page are still alive or run scheduled billing or maintane customers accounts (email customers that their account has to be renewed in next X days, backup for files and folders, creating health cheakers (pinging and retriving your remote servers or web pages regulary to make sure they are working and emailing you if they are down or slow), staring other programs) or anything else that you can imagine. CRON is avaiable only for Linux. However, if you are a Windows user you may use AT command to schedule execution time.
Command Line Interface and GTK : YES! You can do GUI (graphical user interface) applications for your Windows or Linux box with PHP! All you need is PHP Command Line Interface and GTK in a pack. This will allow to create really portable GUI applications and won't require to learn anything else. Just PHP. GTK is avaiable both on Linux and Windows.
Command Line Interface and Your Components : Bet you have already written good nd re-usable classes or functions that you are using in every project. For example, database abastration layer or just a DB connection function or it might be a logging script or date calculation class / library. With PHP Command Line Interface you can reuse them in command line without reinventing the wheel by doing the same in Perl or any other command line enabled language.
Command Line Interface and PHP5 Multithreading : If you have a bottle neck in database or network connection then you can speed up your script up to 1000% just by implementing PHP5 mltithreading. For example, you may spend 10 secod just to establish http connection when you fopening remote http page adn just 1 second to retrive the content. If you need to fopen 1000 pages one by one then you will spend 10*1000+1*1000 = 11000 seconds (3 hours and 3 minutes)! If you run 100 threads then you will spend (10*1000+1*1000)/100 = 110 seconds (less then 2 minutes!). Obviuosly, you will need powerfull enough CPU, enough memory and network bandwidth.
Now start learning by example.
Start with shell>php –v, shell> php phpfile.php.
(You may need to mention full path to php exe and file name)
STDOUT - standard output device.
STDIN - Standard input device.
STDERR - standard Error device.
/*command line arguments. Use arg1 and arg2 in filename.php. Your concern of
Argv and argc array will start from index 1. index 0 will be used for filename.
(It is a difference from CGI way).*/
shell>php filename.php arg1 arg2shell>php
// Using CLI options
// -a = Run interactively. Use enter key after your each input and
after each output by PHP. Ctrl + D to close (will run program). Start with php -a
// enter all the scripts at a time then run. At end use Ctrl+d. Script
//must start from like complete document.
// -l = Syntax-check a PHP script. Will use file php.php from home directory.
shell>php -l ~/php.php
// -w = Display source code after stripping comments
shell>php -w ~/php.php
// -h = Display help
// ask for input
fwrite(STDOUT, "Enter your name: ");
// get input
$name = trim(fgets(STDIN));
// write input back
fwrite(STDOUT, "Hello, $name!");
// remove first argument