Summary of several methods of PHP passing values in CLI mode

Source: Internet
Author: User
Several methods of PHP passing values in CLI mode

PHP itself is a scripting language that can be used efficiently in command-line environments, but how does the CLI (command line Interface) pass in a value?

Here are the 3 ways I know.

A. getopt (Note: PHP 4 >= 4.3.0, PHP 5)

We set up the following documents: test.php

$opt = getopt (' m:n: ');//$value _m= $opt [' m '];//$value _n= $opt [' n '];p rint_r ($opt);

Then run the command

PHP test.php-mvaluem-n Value N

The results are as follows:

For details, please see the manual: http://www.php.net/manual/zh/function.getopt.php

Two. $argv

We modified the test.php file just now, the content is changed to read as follows:

if ($ARGC > 1) {var_dump ($ARGV);}

Run

PHP test.php 1 2 C

The results are as follows:

For details see the manual: http://www.php.net/manual/zh/reserved.variables.argv.php

Three. Linux stdin

In Linux

StdOut standard output, the default is the terminal stdin standard input, the default is the keyboard stderr standard error output, the default is the terminal

So, we modified the test.php file just now.

Fwrite (STDOUT, "Enter Your Name:"), $name = Trim (fgets (STDIN)); Fwrite (STDOUT, "Hello, $name!");

We run

PHP test.php

At this point the screen output: Enter Your name:

We enter in the back: Tiyee and then enter

The screen will now appear hhellow,tiyee

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