PHP explains command-line parameters

Source: Internet
Author: User

PHP CLI mode, you can use $ARGC, $argv to read all the parameters and the number, such as:

[Email PROTECTED]:~/PHP/PHP1/1$ cat go1#!/usr/bin/php<? PHP     Echo $ARGC Php_eol ;     Echo Php_eol ;     Print_r $argv Php_eol;

Add executable permissions to the file:

[Email protected]:~/php/php1/1$ls-Ltotal8-rwxrwxr-x1GHOSTWU GHOSTWU337April A  the: theGo-rw-rw-r--1GHOSTWU GHOSTWU126April A  the: -Go1[email protected]:~/php/php1/1$chmodA +x go1[email protected]:~/php/php1/1$ls-Ltotal8-rwxrwxr-x1GHOSTWU GHOSTWU337April A  the: theGo-rwxrwxr-x1GHOSTWU GHOSTWU126April A  the: -Go1[email protected]:~/php/php1/1$ ./go1 Number of parameters:1Print Parameters: Array ([0] =./go1) [Email protected]:~/php/php1/1$ ./Go1 a b c number of parameters:4Print Parameters: Array ([0] =./Go1 [1] =A [2] =b [3] =c)

If you want to put go1 this file, in any directory of the operating system can be executed, we need to add environment variables, I set up a directory under the home directory Mybin, used to put their own development of the command

Tail -2 ~/bashrcfiexport PATH=~/mybin: $PATH [email protected]:  pwd/home/ghostwu/mybin[email protected]:
Echo $PATH/home/ghostwu/mybin:/home/ghostwu/bin:/home/ghostwu/.local/bin:/usr/local/sbin:/usr/local/bin:/ usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin[email protected]:

Printing $path again, has joined in, this time to copy the developed commands to the ~/mybin directory, and later in any directory of the system can be executed go1

 [email protected]:  ~/mybin$ CP  ~/php/php1/1 /go1. [ Email protected]:  ~/mybin$  Ltotal  8 -rwxrwxr-x 1  GHOSTWU GHOSTWU 126  April 09 : 44   go1  
 [Email protected]:~/mybin$ go1 parameter number:  1   print parameters: Array ([ 0 ] "=/home/ghostwu/ Mybin/go1) [email protected]:  ~/mybin$ CD/ [email protected]: /$ go1 parameter number:  Span style= "color: #800080;" >1   print parameters: Array ([ 0 ] +/home/ Ghostwu/mybin/go1) [email protected]: /$ CD/ tmp[email protected]: /tmp$ go1 parameter number:  1   print parameters: Array ([ 0 ] =/home/ghostwu/mybin/go1)  

In the Linux command line, a lot of commands, or the software has a-v parameter to display the version number, this function how to do?

"' ; if 2 ) $argv [1'-v'Go version is 1.0';     Echo $res. Php_eol;

Is it simple, 3 lines of code are done?

[Email protected]:~/mybin$ go-1.0[email protected]:ls -8  1336 April  :126   go1April   A go1[email:  protected]:

PHP explains command-line parameters

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.