This document will introduce you to PHP's command-line interface (CLI), showing you how to interact with PHP programs at the command prompt.
ExecutableAll PHP distributions, whether compiled from source or pre-built, include a PHP executable by default. This executable can be used to run command-line PHP programs.
To locate this executable on your system, use the following guidelines:
- On Windows, it will be stored in your main PHP installation folder as php.exe or (in older PHP versions) php-cli.exe.
- On Linux, it will be stored in the bin/ sub-directory of your PHP installation directory.
In either case, you can test it to ensure that it works, by calling it with the -v option:
shell> /path/to/php -vPHP 5.0.0 (cli) (built: Jun 1 2005 18:32:10)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.0, Copyright (c) 1998-2004 Zend Technologies
This should return the PHP version number.
A simple PHP CLI program
Once you've located the CLI executable, try it out with a
simple program. Create a text file containing the following PHP code, and save
it as hello.php:
<?php
echo "Hello from the CLI";
?>
Now, try running this program at the command prompt, by invoking the CLI executable and passing it the name of the script to run:
shell> /path/to/phphello.phpHello from the CLI
Using standard input and output
The PHP CLI defines three constants, to make it easier to
interact with the interpreter at the command prompt. These are shown in Table A.
Table A
|
Constant |
Description |
| STDIN |
The standard input device |
| STDOUT |
The standard output device |
| STDERR |
The standard error device |
You can use these constants within your PHP script to accept user input, or display the results of processing and calculation. To understand this better, consider the following script: (Listing A)
Listing A<?php
// ask for input
fwrite(STDOUT, "Enter your name: ");
// get input
$name = trim(fgets(STDIN));
// write input back
fwrite(STDOUT, "Hello, $name!");
?>
Look what happens when you run it:
shell> /path/to/phphello.phpEnter your name: Joe
Hello, Joe!
In this script, the fwrite() function first writes a message to the standard output device asking for the user's name. The user's input is then read into a PHP variable from the standard input device, and incorporated into a string. This string is then printed back to the standard output device with fwrite().
Do you need help with PHP? 







1
dsqfdsdsq - 30/01/07
can someone help me
» Report offensive content
2
Baho - 23/02/07
Hey guys, whats wrong in this code?
Inputs are: 1, 2, 3
Output is:" ", 2, " ", 3, " "
While it was expected to show, just 1, 2, 3 in seperate lines.
Could u just find a little time and send me the solution to beemzet@gmail.com?
» Report offensive content
3
james - 15/05/07
Baho, you've declared the $S variable, but in your echo statement, you're using $s.
$S and $s are different because of their case.
» Report offensive content
4
Vijay - 09/01/08
can anyone help me to solve my problem.
i want to invoke my putty.exe using php script. when I click in the link, then putty will open and waiting for password to enter. I want to set ip address and username also.
I have tried this script
WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("putty.exe", 4, true);
but didn't get result
Please help me
» Report offensive content
5
phphelp - 05/04/08
Wow, thanks for your code!
I had a non-working code & with a minor change (got from your site), it works!!!
Thanks.
» Report offensive content
6
Rafael - 10/05/08
Vjay,
you are missing the dollar sign in front of your first variable declaration.
Best.
» Report offensive content
7
Virus - 25/07/08
I don't get any output on my computer with any thing from a .php file if i runt php -r 'some code' it works but nothing else...
» Report offensive content
8
ana_sh0otz - 20/08/08
i want subscribe my website comment using php but i dont know what the related command and how the comment can view in my website.
» Report offensive content