jose
jose

Reputation: 303

Run PHP script from CLI and pass parameters

I have a PHP script check.php with a function

<?php
function checkValues($arg1, $arg2){

...

}
?>

How can I pass parameter to the function through the Linux command line and run the script?

Upvotes: -1

Views: 1495

Answers (2)

cjhill
cjhill

Reputation: 1054

You need to run PHP:

php /my/script.php arg1 arg2

Then in your PHP file:

<?php
// It's 0 based, but arg1 will be at index 1!
var_dump($argv);

Upvotes: 2

Ankh
Ankh

Reputation: 5718

When running a script from the command line, all parameters passed in after the file name e.g. php my-script.php 123will be in a PHP array called $argv.

array(2) {
    [0]=> string(13) "my-script.php"
    [1]=> int(3) 123
}

Upvotes: 3

Related Questions