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?
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.
#!/usr/bin/php
<?php
$myfile=$argv[1];
See $argv for more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With