How to read parameters through the PHP Command Line

Source: Internet
Author: User

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:

  1. #! C: phpphp.exe-q
  2. <? Php
  3. // For UNIX and Linux platforms
    #! /Usr/local/bin/php-q
  4. Echo "test parameters: n ";
  5. Echo $ _ SERVER ["argc"]. "n ";
  6. // Display the input parameter value, which starts 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 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].


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.