Nov 14, 2016

CLI programming with PHP

Basically, to write cli program with PHP we just use $argv ($argv is enough, $argc - if necessary). $argv contains an array of all the arguments passed to the script when running from the command line.

for($i = 1; $i < $argc; ++$i) {
    echo $argv[$i]. "\n";
In addition, if we 'd expect to get arguments passed like option1=value1

$ php cli_program.php opt1=val1 opt2=val2

we can parse the command line arguments into $_GET by using parse_str() function. <!--php parse_str( implode( '&', array_slice($argv, 1) ), $_GET ); foreach($_GET as $k =--> $v) { echo sprintf("[$k]=$v\n"); } for($i = 1; $i < $argc; ++$i) { echo $argv[$i]. "\n"; } ?> Example:

No comments:

Post a Comment