Monday, 24 September 2018

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

If you want to be able to assign variable names for the values being passed in like php script.php -pvalue1, getopt() is the way to do it. Lets look at a different version of the script now >
$val = getopt("p:");
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 "-name" argument, that's specified by the "name:" value passed to getopt(). The colon (:) means that the parameter must have a value. If you're used to doing something like "script.php?name=Name&roll=Roll" this is an equivalent for the command line.
php script.php --name=test --roll="03 d"
$val = getopt("name:roll:");

0 comments:

Post a Comment