Arguments in PHP

3

I would like to make those scripts that pass arguments.

Ex. php script.php -email [email protected]

Where can I find documentation on this?

PS: I just want to know how to pass these arguments

    
asked by anonymous 01.01.2017 / 23:03

2 answers

7

Use $argv , like this:

var_dump($argv);

Similar to the

Note that $argv[0] always returns the name of the script called, for example in Windows:

C:\Users\Guilherme\Desktop>php teste.php foo bar
Array
(
    [0] => C:\Users\Guilherme\Desktop\teste.php
    [1] => foo
    [2] => bar
)

To remove the first item you can use array_shift , like this:

<?php

$argumentos = $argv;
$script = array_shift($argumentos);

echo $script, '<br>';

echo 'Argumentos:<br>';

var_dump($argumentos);

Documentation:

01.01.2017 / 23:04
2

Use $ argv than are pre-defined variables :

<?php

    var_dump($argv);

Command line:

php args.php -email [email protected]

Output:

array(3) {
  [0]=>
  string(8) "args.php"
  [1]=>
  string(6) "-email"
  [2]=>
  string(31) "[email protected]"
}

A documentation is on the site php.net .

If you want to count the number of arguments, use $ argc , which is also part of of predefined variables :

Command line:

php args.php -email [email protected]

Output:

int(3)

Remarks: $ argv a> and $ argc need that register_argc_argv is enabled to work and what defines one argument from the other is space .

References:

01.01.2017 / 23:08