How to pass arguments to a PHP script via command line?

2

I have a PHP script that takes arguments as follows: script.php -f "valor" .

  • How do I get PHP to pass this argument via the command line?
asked by anonymous 26.03.2014 / 14:25

1 answer

3

To get names and argument values from the command line, use $ argv

command line:

 php cmd.php -p1 v1 -p2 v2

cmd.php

print_r($argv);

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
)
    
26.03.2014 / 14:45