How to correctly implement PHP command-line read Parameters _php Tutorial

Source: Internet
Author: User

If you want to implement PHP command-line read parameters, the CLI can get the number and value of the arguments from $_server[' argc ' and $_server[' argv ']. Let's create a file with the name testargs.php and the script code as follows:

    1. #! C:phpphp.exe–q
    2. < ? PHP
    3. UNIX and Linux platforms should be
      #!/usr/local/bin/php–q
    4. echo "Test get parameter: n";
    5. echo $_server["ARGC"]. " n ";
    6. Displays the parameter values passed in, starting at 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 being best

Test Get Parameters:

4
Always
To
Be
Best

Because we entered a string of words, the script parameters are separated by a space. As a result, PHP counts it as 4 parameters, as described below.

$_server[the "ARGC"] array returns the number of an integral type, representing a total of several parameters entered after the carriage return from the command line.

The result from reading the parameter sample from the PHP command line shows that to access the parameter values that have been passed in, you need to start at index 1. Because the script itself's file already occupies index 0, that is $_server["argv"][0].


http://www.bkjia.com/PHPjc/446037.html www.bkjia.com true http://www.bkjia.com/PHPjc/446037.html techarticle if you want to implement PHP command-line read parameters, the CLI can get the number and value of the arguments from $_server[' argc ' and $_server[' argv ']. We create a file, named testargs.php, Script generation ...

  • 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.