How to correctly implement PHP command line reading parameters_PHP tutorial

WBOY
Release: 2016-07-15 13:33:59
Original
972 people have browsed it

If you want to read parameters from the PHP command line, the CLI can get the parameters from $_SERVER['argc'] and $_SERVER['argv''] Numbers and values. Let’s create another file named testargs.php. The script code is as follows:

  1. #!C:phpphp.exe –q
  2. < ?php
  3. //Under UNIX and Linux platforms it should be
    #!/usr/local/bin/php –q
  4. echo "Test getting parameters: n";
  5. echo $_SERVER[ "argc"]."n";
  6. //Display the incoming parameter value, starting from index 1
  7. echo $_SERVER ["argv"][1]."n";
  8. echo $_SERVER["argv"][2]."n";
  9. echo $_SERVER["argv"][3]."n";
  10. echo $_SERVER["argv"][4]."n";
  11. ?>

Enter the following code on the command line:

C:UsersJohn>testargs.php Always To Be Best

Test Get Parameters:

4
Always
To
Be
Best

Because we entered a string of words, "Always To Be Best", the script parameters are separated by spaces. Therefore, PHP counts it as 4 parameters, which is explained below.

The $_SERVER["argc"] array returns an integer number, representing the total number of parameters entered after pressing Enter on the command line.

The results in the example of reading parameters from the PHP command line have shown that to access the parameter value that has been passed in, you need to start from index 1. Because the file of the script itself already occupies index 0, which is $_SERVER["argv"][0].


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446037.htmlTechArticleIf you want to read parameters from the PHP command line, the CLI can read parameters from $_SERVER['argc'] and $_SERVER[ 'argv''] gets the number and value of parameters. Let’s create another file named testargs.php, with the script code...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template