PHP is most often used in combination with a Web server, to dynamically generate Web site pages. However, this Web server interface isn't the only way to use PHP, because the PHP distribution also includes a command-line interface which can be used to run PHP programs at the command prompt, much like Perl or bash.

This document will introduce you to PHP's command-line interface (CLI), showing you how to interact with PHP programs at the command prompt.

Executable
All 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 -v
PHP 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.php
Hello 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.php
Enter 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? Gain advice from Builder AU forums

Related links

Comments

1

dsqfdsdsq - 30/01/07

can someone help me

while (list($element, $waarde) = each($_POST)) {
if (is_array($element)) {
while (list($e, $w) = each($element)) {
echo($e . " = " . $w. "<br>");
}
} else {
if (!empty($waarde)) {
echo($element . " = " . $waarde . "<br>");
} else {
echo($element . " is !Leeg!<br>");
}
}
}
}

» 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?

<?php
$S = fgets(STDIN);
$P = fgets(STDIN);
$k = fgets(STDIN);
echo "$s\n$P\n$k";
?>

» 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

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

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 ... more

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 ... more

6

Rafael - 05/10/08

Vjay, you are missing the dollar sign in front of your first variable declaration. Best. ... more

Log in


Sign up | Forgot your password?

  • Staff Microsoft shows off IE9 preview

    This week, highlights from Microsoft's MIX10 conference and more in the Roundup. Read more »

    -- posted by Staff

  • Chris Duckett IE9's H.264 vote killed Ogg

    In a split decision by the judges, the winner of the W3C/WHATWG video codec consensus is H.264, taking home the future of video playback on the internet while loser Ogg goes home with nothing but thoughts of what might have been. Read more »

    -- posted by Chris Duckett

  • Staff Google launches Apps Marketplace

    Google launches and app store, while Mozilla plans to re-write its open-source license. More of this week's news in the Roundup. Read more »

    -- posted by Staff

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!