Home >Backend Development >PHP Tutorial >How to correctly implement PHP command line reading parameters_PHP tutorial
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:
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].