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 ($name !== 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

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

How to remove caked on stickers from old game carts

How to pass parameters to your PHP script via the command line

jPhotoFrame new layout engine explained with examples

Atari Lynx Multi SD Card cartridge review

Jersey JAX-RS filters and interceptors execution order when throwing Exceptions

Why I'll never renew a domain with GoDaddy again

Fake AirPods - HBQ-i7 TWS wireless headset teardown

How to fix Postman error - Failed to import data: format not recognized

Switching out macOS Mail for Microsoft Outlook

Using DeoxIT to repair old game catridges

Recent Galleries

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Tiny Arcade revision 6 kit assembly and decal application

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity