Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading positional parameters using PHP

I am using a .php page just like a shell script. The only problem is that I do not know how to pass the positional parameters to PHP page.

#!/usr/bin/php
<?php

$myfile="balexp.xml";

The following does not work in this php page and does work in the shell script.

myvar=$1

How do I pass variables to a php page?

like image 662
shantanuo Avatar asked Jan 21 '23 21:01

shantanuo


2 Answers

You need to make use of the array $argv.

Run the following CLI PHP passing it command line arguments:

#!/usr/bin/php
<?php
var_dump($argv);
?>

On running:

$ chmod u+x a.php
$ ./a.php
array(1) {
  [0]=>
  string(7) "./a.php"
}

$ ./a.php foo
array(2) {
  [0]=>
  string(7) "./a.php"
  [1]=>
  string(3) "foo"
}

$ 

Clearly $argv[0] contains the name of the script, $argv[1] contains the first command line argument.

The manual has all the details on how to handle command line arguments.

like image 67
codaddict Avatar answered Jan 29 '23 10:01

codaddict


#!/usr/bin/php
<?php

$myfile=$argv[1];

See $argv for more.

like image 23
ariefbayu Avatar answered Jan 29 '23 10:01

ariefbayu