Home > Backend Development > PHP Tutorial > Two ways to pass parameters to php from the command line_PHP Tutorial

Two ways to pass parameters to php from the command line_PHP Tutorial

WBOY
Release: 2016-07-13 10:29:51
Original
959 people have browsed it

##$argv or $argc
$argv is an array containing the arguments passed to the current script when run from the command line. $argv[0] is the script file name.
$argc contains the number of arguments passed to the current script when run from the command line. The script's filename is always passed as an argument to the current script, so the minimum value of $argc is 1.
The two variables are only available when register_argc_argv is turned on.

Note: $argv and $argc must be declared as global variables inside class methods or functions

<?<span>php
</span><span>class</span><span> A
{
    </span><span>public</span> <span>static</span> <span>function</span><span> b()
    {
        </span><span>var_dump</span>(<span>$argv</span><span>);
        </span><span>var_dump</span>(<span>isset</span>(<span>$argv</span><span>));
    }
}

A</span>::b();
Copy after login
<?<span>php
printarg();

</span><span>function</span><span> printarg(){
</span><span>global</span> <span>$argc</span>,<span>$argv</span><span>;
</span><span>print</span>(<span>$argc</span>."个参数\n"<span>);
</span><span>print_r</span>(<span>$argv</span><span>);
}</span>
Copy after login


##getopt
array getopt ( string $options [, array $longopts ] )
options Each character in this string will be treated as an option character, matching the options passed to the script. Begins with a single hyphen (-). For example, an option string "x" identifies an option -x. Only a-z, A-Z and 0-9 are allowed. longopts Array of options. Each element in this array will be treated as an options string, matching the options passed to the script with two hyphens (--). For example, the long option element "opt" identifies an option --opt.

options may contain the following elements:
1. A single character (does not accept a value)
2. A character followed by a colon (this option requires a value)
3. Followed by two characters colon characters (the value of this option is optional) The value of the
option is the first parameter after the string. It doesn't mind if there are spaces before the value.


Return value:
This function returns an option/parameter pair, and returns FALSE on failure.


Note: The value of the
option does not accept spaces (" ") as delimiters.
The formats of options and longopts are almost the same, the only difference is that longopts needs to be an array of options (each element is an option), while options needs a string (each character is an option).
Parsing of options will terminate at the first non-option found, anything after that will be discarded.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/769342.htmlTechArticle##$argv or $argc $argv contains the parameters passed to the current script when running from the command line array. $argv[0] is the script file name. $argc contains what is passed to when running from the command line...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template