4
I have a script PHP which receives arguments as follows: script.php -f "valor"
.
- How do I make PHP pass this argument via command line?
4
I have a script PHP which receives arguments as follows: script.php -f "valor"
.
6
To take the names and values of the arguments from the command line use $argv
command line:
php cmd.php -p1 v1 -p2 v2
cmd.php
print_r($argv);
Being that the first item of $argv
is the name of the script. The output of cmd.php will be:
Array
(
[0] => cmd.php
[1] => -p1
[2] => v1
[3] => -p2
[4] => v2
)
1
You can be using the function getopt()
which is nothing more than an abbreviation of "Get Options".
<?php
$args = getopt('f:'); // refere-se ao parâmetro "-f"
print_r($args); // será imprimida e seu retorno será um Array
?>
this function will return an array of options/arguments in the Command Prompt (or any other shell/console)
php script.php -f "hello world"
Array
(
[f] => ola mundo
)
$argv
<?php
print_r($argv); // obtêm quaisquer argumentos passados (incluindo o próprio arquivo)
?>
php script.php teste1 teste2 teste3
Array
(
[0] => script.php
[1] => teste1
[2] => teste2
[3] => teste3
)
make use of the getopt()
because with it you can pre-define specific parameters, causing it nay return any argument "out of the ordinary" (He gets what you want him to get!). Thus having a control of the content of Array
generated.
It is worth mentioning some points where one must respect parameters, as the name already says!
as for example the use of "-f", where to be passed "-ff" as parameter
php script.php -ff "ola mundo"
its result will be an "f" as value and not what was passed after it.
Array
(
[f] => f
)
Browser other questions tagged php terminal
You are not signed in. Login or sign up in order to post.
How we should format questions and answers?
– Augusto Vasques