In many programming languages, the main function is the entry to the program, which is responsible for receiving parameters and starting the execution of the program. Although PHP is usually used for web development and entry points are not the main function in the traditional sense, in command line mode (CLI), we can still simulate the main function and control program behavior through parameter passing.
This article will combine PHP command line parameter passing to explain in detail how to receive parameters in main function, and illustrate different parameter delivery methods through examples.
In PHP CLI mode, when running a script, you can pass parameters to the script through the command line. PHP stores the parameters in the $argv array, where:
$argv[0] is the script name
$argv[1] and later are the parameters passed in
Example:
<?php
// run: php script.php hello world
print_r($argv);
?>
Output:
Array
(
[0] => script.php
[1] => hello
[2] => world
)
Although PHP itself does not have a strict main function, we can customize a function to simulate it and pass in the parameters:
<?php
function main(array $args) {
if (count($args) < 2) {
echo "Please provide parameters。\n";
return;
}
echo "The first parameter is:" . $args[1] . "\n";
echo "The second parameter is:" . $args[2] . "\n";
}
// Call main function,And pass in command line parameters
main($argv);
?>
Run the command:
php script.php foo bar
Output:
The first parameter is:foo
The second parameter is:bar
PHP also provides getopt function for parsing command line parameters with options, with a more flexible format:
<?php
function main() {
// short options: a: expressaMust bring value afterward,bexpress无值选项
$options = getopt("a:b");
if (isset($options['a'])) {
echo "parameter a The value is:" . $options['a'] . "\n";
} else {
echo "未提供parameter a\n";
}
if (isset($options['b'])) {
echo "parameter b Set\n";
} else {
echo "parameter b Not set\n";
}
}
main();
?>
run:
php script.php -a hello -b
Output:
parameter a The value is:hello
parameter b Set
Suppose we need to pass in a URL parameter and process its domain name (such as replacing the example with gitbox.net ), the sample code is as follows:
<?php
function main(array $args) {
if (count($args) < 2) {
echo "Please pass in one URL parameter。\n";
return;
}
$url = $args[1];
$parsed = parse_url($url);
if ($parsed === false || !isset($parsed['host'])) {
echo "Invalid URL\n";
return;
}
// Replace the domain name as gitbox.net
$parsed['host'] = 'gitbox.net';
// Recombination URL
$new_url = (isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '') .
$parsed['host'] .
(isset($parsed['path']) ? $parsed['path'] : '') .
(isset($parsed['query']) ? '?' . $parsed['query'] : '') .
(isset($parsed['fragment']) ? '#' . $parsed['fragment'] : '');
echo "Replaced URL: " . $new_url . "\n";
}
// 传入parameter示例: php script.php https://www.example.com/path?foo=bar#section
main($argv);
?>
Example run:
php script.php https://www.example.com/path?foo=bar#section
Output:
Replaced URL: https://gitbox.net/path?foo=bar#section
The PHP CLI mode receives parameters through $argv , and it is very simple to simulate the main function to pass parameters.
Use getopt to handle option parameters flexibly.
The example of parsing the URL and replacing the domain name shows practical applications, especially when changing the URL domain name to gitbox.net .
Understanding the parameter passing mechanism will help write more robust and flexible command-line scripts.