Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

I've recently received some comments on a previous article I wrote about scheduling PHP scripts using Cron that were asking how to pass parameters to the script. In this article I look at a number of different methods this can be done.

Note that if register_argc_argv is disabled (set to false) in php.ini, these methods will not work.

First lets see what will 100% not work. If you're used to passing command line parameters to a PHP script in a web browser using a query parameter (and then looking it up using $_GET), you may be trying to do something like this...
 Command Line
php script.php?param1=value1


Of course PHP will promptly reject this with an error like this...
 Error
Could not open input file: script.php?param1=value1


The reason that happens is because PHP is looking for a file called "script.php?param1=value1" but the script is actually called "script.php". The file system is not a web server so it doesn't know how to handle query parameters. A different approach is required.

Lets look at the 'classic' method to pass command line parameters to a script - using $argc and $argv.

Here's a PHP script that displays all of the command line arguments that were passed to it...
 script.php
<?php
if (isset($argc)) {
for ($i = 0; $i < $argc; $i++) {
echo "Argument #" . $i . " - " . $argv[$i] . "\n";
}
}
else {
echo "argc and argv disabled\n";
}
?>


To pass command line arguments to the script, we simply put them right after the script name like so...
 Command Line
php script.php value1 value2


The output produced is...
 Output
Argument #0 - script.php
Argument #1 - value1
Argument #2 - value2


Note that the 0th argument is the name of the PHP script that is run. The rest of the array are the values passed in on the command line. The values are accessed via the $argv array. This approach works, but it is very simplistic and doesn't play well if you're looking to transition from a query parameter way of passing in values to your script. With this approach there is no way to give names the the command line arguments being passed in.



If you want to be able to assign variable names for the values being passed in, getopt() is the way to do it. Lets look at a different version of the script now...
 script.php
<?php
$val = getopt("p:");
if ($val !== false) {
echo var_export($val, true);
}
else {
echo "Could not get value of command line option\n";
}
?>


Lets run this script like this...
 Command Line
php script.php -pvalue1


The output produced is...
 Output
array (
'p' => 'value1'
)


There are some major differences here. First with getopt() you must specify which command line argument you want to retrieve. In the case of this script, it looks for the "-p" argument, that's specified by the "p:" value passed to getopt(). The colon (:) means that the parameter must have a value. If you're used to doing something like "script.php?p=value1" this is an equivalent for the command line.

It's possible to pass multiple values in as well e.g.
 Command Line
php script.php -pvalue1 -pvalue2


In this case, the value changes to an array, so it's important to check the value type before using it.
 Output
array (
'p' =>
array (
0 => 'value1',
1 => 'value2',
),
)


If you want to pass in multiple differently named parameters e.g. "p" and "q", you change the getopt() call like this...
 script.php
$val = getopt("p:q:");


Then running the script like this...
 Command Line
php script.php -pvalue1 -qvalue2


...produces this output...
 Output
array (
'p' => 'value1',
'q' => 'value2',
)


The above way of using getopt() limits your to using single character parameter names, what if you wanted to use a parameter name like "name"? This is also possible, we just change getopt() call to this...
 script.php
$val = getopt(null, ["name:"]);


When using long parameter names, the way that the PHP script is run changes slightly, in this case we pass the parameter like so...
 Command Line
php script.php --name=Igor


The output is then...
 Output
array (
'name' => 'Igor',
)


This can be expanded to multiple differently named parameters too. Passing the same named parameter multiple times also results in the value being of an array type, same as described earlier. It is also possible to pass values with a space in them by putting them in quotes.

This article doesn't cover all of the possibilities with getopt() but it gives a starting point and hopefully now you can convert your script that uses the usual query parameters with $_GET to something that uses one of the approaches outlined above.

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.