To read parameters through the PHP Command Line, CLI can obtain the number and value of parameters from $ _ SERVER ['argc '] and $ _ SERVER ['argv. Create another file named testargs. php. The script code is as follows:
- #! C: phpphp.exe-q
- <? Php
- // For UNIX and Linux platforms
#! /Usr/local/bin/php-q
- Echo "test parameters: n ";
- Echo $ _ SERVER ["argc"]. "n ";
- // Display the input parameter value, which starts from Index 1
- Echo $ _ SERVER ["argv"] [1]. "n ";
- Echo $ _ SERVER ["argv"] [2]. "n ";
- Echo $ _ SERVER ["argv"] [3]. "n ";
- Echo $ _ SERVER ["argv"] [4]. "n ";
- ?>
Enter the following code in the command line:
C: UsersJohn> testargs. php Always To Be Best
Obtain test parameters:
4
Always
To
Be
Best
Because we input a string of words, which are "Always To Be Best", the script parameters are separated by spaces. Therefore, PHP counts it as four parameters, which are described below.
The $ _ SERVER ["argc"] array returns an integer value, which indicates that several parameters are input after you press enter from the command line.
The result of reading the parameter example from the PHP Command Line shows that to access the passed parameter value, you must start from Index 1. Because the script file occupies index 0, that is, $ _ SERVER ["argv"] [0].