I want to pass parameters from PHP Command Line Interface, and then read in the values using PHP script, something like this:
<?php
$name1 = $argv[1];
echo $name1;
?>
I pass the variable from CLI like this:
C:\xampp\php\php.exe name.php Robby
The above works, I get Robby as the output.
But I want to do something like this:
C:\xampp\php\php.exe name.php -inputFirstName="Robby"
So that the user is well informed to enter the correct parameters in the correct places. What is the appropriate way to parse these parameters?
When calling a PHP script from the command line you can use $argc to find out how many parameters are passed and $argv to access them. For example running the following script:
Like this:-
Will give the following output
See $argv and $argc for further details.
To do what you want, lets say
You would need to explode the argument on the equals sign:-
That way you can have whatever you want in front of the equals sign without having to parse it, just check the key=>value pairs are correct. However, that is all a bit of a waste, just instruct the user on the correct order to pass the arguments.
While the answer is correct and you could do the parsing by hand, PHP also offers the
getopt()
function that might actually provide useful here.There's also object-oriented alternatives (written in PHP, available in a number of libraries) that might turn out to be what you need. Googling for "php getopt" will yield helpful results.
If you don't mind using a library, I suggest you take a look at The Console Component by Symfony.
It can be used to create command line applications and supports the use of Arguments & Options.
The documentation page contains a couple of excellent examples to get you started.
Of course under the hood it uses the same techniques as explained by vascowhite.
I use this fairly concise method:
Which would handle a call such as:
By sending it into $_GET you automatically handle web or CLI access.
For commentary, this is doing the following:
You can parse the user input on your program looking for specific strings such as
-inputFirstName="x"
(using regular expressions, for example) and then set the correct variable to x.The
getopt()
function is probably the most correct answer in the case of the question. Especially since it was made platform independent with PHP 5.3. In the particular case of this question and parsing multiple parameters, one way to leverage this function would be as follows:The call to set $inputFirstName with the value "Robby" would be:
Explanation
Default values for all expected parameters are set in the
$defaultValues
array. Input sent through via command line arguments are collected by PHP'sgetopt
function and stored by the$givenArguments
. Note that the colon (:
) at the end of the"inputFirstName:"
string indicates that this is a required argument. Without a colon here, only the presence of the argument would be detected, not the actual value (more information in the PHP Manual). Merging these two arrays together on the third line results in array with all expected parameters being set with either default values or arguments provided from the command line/terminal if they are available.