Php – Run php script from command line with variable

command-linephp

I want to run a PHP script from the command line, but I also want to set a variable for that script.

Browser version: script.php?var=3

Command line: php -f script.php (but how do I give it the variable containing 3?)

Best Solution

Script:

<?php

// number of arguments passed to the script
var_dump($argc);

// the arguments as an array. first argument is always the script name
var_dump($argv);

Command:

$ php -f test.php foo bar baz
int(4)
array(4) {
  [0]=>
  string(8) "test.php"
  [1]=>
  string(3) "foo"
  [2]=>
  string(3) "bar"
  [3]=>
  string(3) "baz"
}

Also, take a look at using PHP from the command line.