Handling Command Line Parameters for PHP Script (Turn Parameters into GET/POST)


The PHP can be used as a scripting language for command line tools. We can access the $argc and $argv these two variables to access the number of arguments and the command line parameters respectively. For example:

1
2
3
<php
echo "The number of Argument is $argc\n";
print_r($argv);
<php
echo "The number of Argument is $argc\n";
print_r($argv);

Let’s save it as ‘test.php’ and if we call it in command line:

1
2
3
4
5
6
? php test.php
The number of Argument is 1
Array
(
    [0] => php.php
)
? php test.php
The number of Argument is 1
Array
(
    [0] => php.php
)

And let’s give a few more parameters:

1
2
3
4
5
6
7
8
9
$ php test.php 1 2 "3 4"
The number of Argument is 4
Array
(
    [0] => test-php.php
    [1] => 1
    [2] => 2
    [3] => 3 4
)
$ php test.php 1 2 "3 4"
The number of Argument is 4
Array
(
    [0] => test-php.php
    [1] => 1
    [2] => 2
    [3] => 3 4
)

As we can see, the first parameter is always the name of the PHP script, and we can discard it if we don’t want that via the php array_shift function:

1
2
3
4
5
<php
$argc --;
array_shift($argv);
echo "The number of Argument is $argc\n";
print_r($argv);
<php
$argc --;
array_shift($argv);
echo "The number of Argument is $argc\n";
print_r($argv);

Let’s test it again:

1
2
3
4
5
$ php test.php
The number of Argument is 0
Array
(
)
$ php test.php
The number of Argument is 0
Array
(
)

And give it a few comnmand line parameters:

1
2
3
4
5
6
7
8
$ php test.php 1 2 "3 4"
The number of Argument is 3
Array
(
    [0] => 1
    [1] => 2
    [2] => 3 4
)
$ php test.php 1 2 "3 4"
The number of Argument is 3
Array
(
    [0] => 1
    [1] => 2
    [2] => 3 4
)

Turn Command Line Parameters into GET or POST

We can use the following to parse the command line paramters to $_GET via the parse_str function (parse the string into variables):

1
2
3
4
$argv = array_slice($argv, 1);
// or 
// array_shift($argv);
parse_str(implode('&', $argv), $_GET);
$argv = array_slice($argv, 1);
// or 
// array_shift($argv);
parse_str(implode('&', $argv), $_GET);

For example:

1
2
3
4
5
6
7
8
<?php
$argc --;
array_shift($argv);
parse_str(implode('&', $argv), $_GET);
$id = $_GET['id'] ?? "";
$val = $_GET['val'] ?? "";
echo "id is $id\n";
echo "val is $val\n";
<?php
$argc --;
array_shift($argv);
parse_str(implode('&', $argv), $_GET);
$id = $_GET['id'] ?? "";
$val = $_GET['val'] ?? "";
echo "id is $id\n";
echo "val is $val\n";

And the PHP will pick up the command line parameters and convert them to $_GET or $_POST in the format of “key=value”

1
2
3
$ php test.php id=123 val=456
id is 123
val is 456
$ php test.php id=123 val=456
id is 123
val is 456

We can also pass the array, for example:

1
2
3
4
5
6
<?php
$argc --;
array_shift($argv);
parse_str(implode('&', $argv), $_GET);
$id = $_GET['id'] ?? "";
print_r($id);
<?php
$argc --;
array_shift($argv);
parse_str(implode('&', $argv), $_GET);
$id = $_GET['id'] ?? "";
print_r($id);

And we can pass arrays:

1
2
3
4
5
6
$ php test.php id[]=1 id[]=2
Array
(
    [0] => 1
    [1] => 2
)
$ php test.php id[]=1 id[]=2
Array
(
    [0] => 1
    [1] => 2
)

--EOF (The Ultimate Computing & Technology Blog) --

GD Star Rating
loading...
506 words
Last Post: How RNG Software Determines Online Progressive Jackpot Winners?
Next Post: Teaching Kids Programming - Contiguously Increasing Numbers (Depth First Search and Breadth First Search Algorithm)

The Permanent URL is: Handling Command Line Parameters for PHP Script (Turn Parameters into GET/POST)

Leave a Reply