With the continuous development of PHP, PHP is no longer just a language used to develop web applications. The latest version of PHP also supports command line development. In this article, we will take an in-depth look at how to use PHP7.0 for command line development.
PHP Command Line Development
Command line development mainly focuses on automating scripts for web applications, thereby improving the efficiency and quality of web applications. PHP has powerful features in command line development, such as:
Installation of PHP command line environment
Before using PHP for command line development, you need to ensure that PHP has been correctly installed on the computer. You can check the PHP version information through the following command:
php -v
If the output version number is PHP7.0 or higher, it means the installation has been successful.
PHP command line script
PHP command line script can be run through the command line, and its extension is usually .php or .php-cli. The following is a simple PHP command line script example, which can be saved as a test.php file:
#!/usr/bin/env php <?php echo 'Hello, World!' . PHP_EOL;
When running on Linux/Unix, you need to set execution permissions on the file first:
chmod x test.php
Then you can execute the script using the following command:
./test.php
Command line parameters
Command line Parameters are an important part of command line scripts. Command line parameters can help us pass different parameters to the script to make it more powerful.
In PHP, you can use the global variable $_SERVER['argv'] to obtain command line parameters. $_SERVER['argv'] is an array containing all command line parameters. $_SERVER['argv'][0] is the name of the script, and other parameters start with $_SERVER'argv'.
The following is an example of a PHP script that accepts command line parameters:
#!/usr/bin/env php <?php if(isset($_SERVER['argv'][1])) { $name = $_SERVER['argv'][1]; } else { $name = 'World'; } echo 'Hello, ' . $name . '!' . PHP_EOL;
When running this script, you can pass any parameters to the script by appending the script name:
./test.php Tom
This command will output:
Hello, Tom!
PHP command line options
In addition to the command line parameters In addition, PHP also supports command line options. Command line options allow us to specify some flags to control the behavior of command line scripts.
PHP’s built-in getopt() function can help us parse command line options. The getopt() function accepts two parameters, the first parameter is a string containing the options, and the second parameter is an array used to specify short options and long options.
The following is an example of a PHP script using the getopt() function:
#!/usr/bin/env php <?php $shortopts = ""; $shortopts .= "v::"; $shortopts .= "t:"; $longopts = array( "version::", "text:", ); $options = getopt($shortopts, $longopts); if(isset($options['v']) || isset($options['version'])) { echo 'Version 1.0.0' . PHP_EOL; exit; } if(isset($options['t']) || isset($options['text'])) { $text = isset($options['t']) ? $options['t'] : $options['text']; echo 'Text: ' . $text . PHP_EOL; exit; } echo 'Usage: test.php [-v|--version] [-t|--text <text>]' . PHP_EOL;
In the above example, -v and --version are options without parameters, which are used to print the version number . And -t and --text are options with parameters that accept a text parameter and print it out.
Here are some examples when running this script:
./test.php -v
./test.php --version
./test.php -t "Hello, World!"
./test.php --text "Hello, World!"
Conclusion
PHP7.0 not only supports the development of web applications, but also commands There have been great improvements in the field of row development. In this article we introduced some basic knowledge of PHP command line development, such as command line scripts, command line parameters and command line options. I hope these contents can help you gain a deeper understanding of PHP command line development.
The above is the detailed content of How to use PHP7.0 for command line development?. For more information, please follow other related articles on the PHP Chinese website!