Instructions for customizing parameter Passing in PHP CLI commands

Source: Internet
Author: User
Tags php cli
Whether it's compiled from source or pre-created versions of PHP, it comes with a PHP executable by default. This executable can be used to run the command-line PHP program.

To find this executable file in the system, follow these steps: Windows: Placed in the main PHP installation directory, the file name is Php.exe or (in the old version of PHP) is Php-cli.exe. Linux: Saved in the bin/subdirectory of the PHP installation directory.

Note: The CLI mode and CGI mode runtime php.ini are not the same set of configurations and need to be configured separately. In any system, it needs to be tested to make sure it works correctly by calling it with the-v parameter:

shell>/path/php.exe-vphp 5.0.0 (CLI) (Built:jun 1 2005 18:32:10) Copyright (c) 1997-2004 the PHP groupzend Engine v2. 0.0, Copyright (c) 1998-2004 Zend Technologies
It should return the version number of PHP.

using CLI commands a simple PHP CLI program, named hello.php

 
      
Now, try running the program at a command-line prompt by invoking the CLI executable and providing the script's file name:
Shell>/path/php.exe/example/hello.phphello from the CLI
use of standard inputs and outputsThe PHP CLI defines three constants to make it easier to interact with the interpreter at the command-line prompt. These constants are shown in the following table
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.

Examples of Use:

 
      Output:d:\>\wamp\bin\php\php5.3.0\php.exe  \tools\index.phpenter your Name:kkkhello, kkk!
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 ().command line custom variables1 "$argv | $ARGC" 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 that are specifically designed to achieve this purpose: one is$argvVariable, which saves the parameters passed to the PHP script as a separate array element through the command line, and the other is$ARGCVariable that is used to hold the number of elements in the $ARGV array. Examples of Use:
 
      Output:d:\>\wamp\bin\php\php5.3.0\php.exe  \tools\index.php BAC Dddarray (    [0] = \tools\index.php    [1] = BAC    [2] = = DDD)
As you can see from the output, the value passed to the index.php is automatically shown as an array element in the $ARGV. Note that the first argument $argv is always the script's own name.

Note: We can also use console_getopt the Pear class adds more complex command-line arguments to PHP.

command line custom variable 2 "Receive parameters using console_getopt "

Note: This variable is only available when REGISTER_ARGC_ARGV is open

Getopt ($option, $longopts)///First $Option receives-H vb second parameter receives--require SSS

Usage examples

 
      Ouput:d:\>\wamp\bin\php\php5.3.0\php.exe  \tools\index.php-f "value for F"-v-a--required value--optional= " Optional value "--option Willarray (6) {  [" F "]=>  string (one)" value for F "  [" V "]=>  bool (FALSE)  ["A"]=>  bool (false)  ["Required"]=>  string (5) "Value"  ["optional"]=>  string (+) "Optional value"  ["option"]=>  bool (FALSE)}
command-line variables3 "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.

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-ainteractive mode Enabled
 
      
Or, you cando not use -aparameter to invoke the CLI executable file 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
 
      
 
      
       
  12-jul-2005 06:54:04
 
      
  • 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.