Introduction to PHP CLI (GO)

Source: Internet
Author: User
Tags echo date php cli
Introduction to PHP CLI (GO)

All PHP distributions, whether compiled from source code or pre-created versions, are provided with a PHP executable by default. This executable can be used to run the command-line PHP program.

To find this executable file on your system, follow the steps below:

In the Windows operating system, it is placed in the main PHP installation directory, the file name is Php.exe or (in the old version of PHP) is Php-cli.exe.

In the Linux operating system, it is stored in the PHP installation directory under the bin/subdirectory.

Regardless of the operating system, you need to test it to make sure it works correctly by calling it with the-v parameter:

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

It should return the version number of PHP.

A Simple PHP CLI program
Once you have found the CLI executable, you can use it in a simple program. Create a simple text file that contains the following PHP code and save it as hello.php:

echo "Hello from the CLI";
?>

Now, try running the program at a command-line prompt by invoking the CLI executable and providing the script's file name:

Shell>/path/to/phphello.php
Hello from the CLI

Use of standard inputs and outputs
The PHP CLI defines three constants to make it easier to interact with the interpreter at the command-line prompt. These constants are shown in Table A.

Form a

Constant Description
Stdin Standard input Devices
STDOUT Standard output devices
STDERR Standard error Device

You can use these three constants in your PHP scripts to accept user input, or to display the results of processing and calculations. To better understand this, take a look at the following script (List a):

List A
Ask for input
Fwrite (STDOUT, "Enter Your Name:");

Get input
$name = Trim (fgets (STDIN));

Write input back
Fwrite (STDOUT, "Hello, $name!");
?>
Look how 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. It then reads the user input information obtained from the standard input device into a PHP variable and merges it into a string. The string is then printed out to the standard output device using fwrite ().

Using command-line arguments
It is common practice to enter program parameters at the command line to change how they are run. You can also do this to the CLI program. The PHP CLI has two special variables for this purpose: one is the $ARGV variable, which saves the arguments passed to the PHP script as a separate array element through the command line, and the $ ARGC variable, which is used to hold the number of elements in the $ARGV array.

It is very simple to write a piece of code that reads $ARGV and processes the parameters contained in a PHP script. Try the example script in list B to see how it works:

List B
Print_r ($ARGV);
?>

Run this script by passing it some arbitrary values, and check the output:

Shell>/path/to/phptest.php Chocolate 276 "killer tie, dude!"
Array
([0] = test.php
[1] = chocolate
[2] = 276
[3] = Killer tie, dude!
)

As you can see from the output, the value passed to the test.php is automatically shown as an array element in the $ARGV. Note that the first argument $argvis is always the script's own name.

The following is a more complex example (List C):

List C
Check for all required arguments
First argument is always name of script!
if ($ARGC! = 4) {
Die ("Usage:book.php ");
}

Remove first argument
Array_shift ($ARGV);

Get and use remaining arguments
$checkin = $argv [0];
$nights = $argv [1];
$type = $ARGV [2];
echo "You have requested a $type the $nights nights, checking in on $checkin. Thank for your order! ";
?>

Here is an example of its usage:

Shell>/path/to/phpbook.php 21/05/2005 7 single
You are requested a single and 7 nights, checking in on 21/05/2005. Thank for your order!

Here, the script first checks the $ARGC to ensure that the number of independent variables meets the requirements. It then extracts each of the arguments from the $argv and prints them out to the standard output device.

Note: You can use the Console_getopt pear class to add more complex command-line arguments to PHP.

Using CLI parameters
In addition to using the command line to pass PHP script parameters, you can also pass the PHP CLI parameter to change how it works. Table B is a list of some important parameters:

Form B

Parameters Description
-A Running run interactively interactively
-C Path reads PHP's. ini file from path
-N Run directly without reading PHP's. ini file
-M List the compiled modules
-I. Display information about PHP builds
-L Check the syntax of the PHP script
-S Display the source code in a colored fashion
-W Display the source code after removing the comment
-H Show Help

Interactive mode
You can also interactively use the PHP CLI, which is the input command, to get results immediately. To get this effect, you just need to invoke the CLI executable with one parameter, just like this:

Shell>/path/to/php-a

You will see a blank line where you can enter the PHP code. Look:

Shell>/path/to/php-a
Interactive mode Enabled
Echo Mktime ();
1121187283
echo;
4
Exit ();
Shell>

Alternatively, you can invoke the CLI executable without using the-a parameter and enter the complete script or code snippet directly. Use -D to end the code snippet and let the CLI execute it. See the following example:

Shell>/path/to/php
echo Date ("D-m-y h:i:s", Time ());
?>

12-jul-2005 06:54:04

This is the command line for PHP, and now you should have enough knowledge of the PHP CLI and start using it.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.