5
Guys I’d like to do those scripts that pass arguments.
Ex. php script.php -email [email protected]
Where can I find documentation about this?
PS: I just want to know how to pass these arguments
5
Guys I’d like to do those scripts that pass arguments.
Ex. php script.php -email [email protected]
Where can I find documentation about this?
PS: I just want to know how to pass these arguments
6
Use $argv
, thus:
var_dump($argv);
Is similar to c
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
, thus:
<?php
$argumentos = $argv;
$script = array_shift($argumentos);
echo $script, '<br>';
echo 'Argumentos:<br>';
var_dump($argumentos);
Assuming you want to take specific elements you won’t even need the array_shift
, if you know you want to take the first parameter and the second for example, just check with an IF() the amount (use the $argc
to obtain the amount of parameters passed) and then take the specific index:
if ($argc >= 3) {
$primeiro = $argv[1];
$segundo = $argv[2];
} else {
die('Parâmetros incorretos');
}
Documentation:
2
Utilize $argv that are predefined variables:
<?php
var_dump($argv);
Command line:
php args.php -email [email protected]
Exit:
array(3) {
[0]=>
string(8) "args.php"
[1]=>
string(6) "-email"
[2]=>
string(31) "[email protected]"
}
To documentation is on the website php.net.
If you want to count the number of arguments use $argc, which is also part of predefined variables:
Command line:
php args.php -email [email protected]
Exit:
int(3)
Remarks: so much $argv and $argc need that register_argc_argv is enabled to function and what defines one argument from the other is space.
References:
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
face if I need to ask questions like. What’s your name ? and expect an answer?
– user50712
@Lucasbicalleto a question would be an input that waits for the user to type?
– Guilherme Nascimento
Yes, how can I do that and recover the data? sorry to abuse the questions :/
– user50712
Put as a new question please, as soon as you do I already put a new answer there.
– Guilherme Nascimento