Reading Positional Parameters Using PHP

I use the .php page as a shell script. The only problem is that I donโ€™t know how to pass positional parameters to a PHP page.

#!/usr/bin/php <?php $myfile="balexp.xml"; 

The following does not work on this php page: it works in a shell script.

 myvar=$1 

How to pass variables to php page?

+4
source share
4 answers
 #!/usr/bin/php <?php $myfile=$argv[1]; 

See $ argv for more details.

+3
source

You need to use the $argv array.

Run the following PHP CLI by passing command line arguments to it:

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

At startup:

 $ 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" } $ 

It is clear that $argv[0] contains the name of the script, $argv[1] contains the first command line argument.

the manual contains all the information on how to handle command line arguments.

+4
source

if you enabled register_argc_argv you can use $argv

 your_script.php haha hehe <? echo $argv[0] <-- your_script.php $myfile=$argv[1]; echo $myfile; <-- return haha ?> 

more details: http://www.php.net/manual/en/features.commandline.usage.php

+1
source

You pass positional parameters through the $ argv convention. See http://php.net/manual/en/features.commandline.usage.php .

+1
source

Source: https://habr.com/ru/post/1332861/


All Articles