faqts : Computers : Programming : Languages : PHP : Installation and Setup : CGI

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

58 of 65 people (89%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How do I pass variables to a PHP script from the command line??
Is it possible to pass variables to a php script from the command line? If so How?

Feb 21st, 2001 04:30
php freak, Emiliano Martínez Luque, Anonymous,


To pass a variable to PHP from the command line use the following
>/usr/local/bin/php -q /Path_to_the_script/name_of_script.php3 
argument1, argument2
Which can be retriebed from php using $argv[i]. Where i is the number 
of the argument in the array of arguments.   
Example:
>/usr/local/bin/php -q /Path_to_the_script/name_of_script.php3 
argument1, argument2
for the following script:
<?php
echo("$argv[1] - $argv[2]");
?>
will print:
argument1 - argument2
Comment: 
From my own tests with this I´ve found out that $argv[0] stores the 
line with wich you call the script.
php freak:
Also,it is possible to send your variables on the command line
as $HTTP_GET_VARS to your script with php4.
to do so: 
php -q /Path_to_the_script/NameOfScript.php "&key=value&key2=value2..."
In your script you catch this variables as $HTTP_GET_VARS["key"],
$HTTP_GET_VARS["key2"], ...