Pass Variables to PHP Scripts via Command Line
When running PHP scripts from the command line, it's necessary to pass variables to control the script's behavior. However, the approach commonly used for web pages, myfile.php?type=daily, may not work.
Parameter Passing via $argv
To pass variables from the command line, use the $argv array. When running php myfile.php daily, $argv[1] will contain the value "daily". This is in contrast to web pages, where query string parameters are stored in $_GET.
Alternatives for Web Page Usage
If the PHP script also serves as a web page, there are two possible solutions:
#!/bin/sh wget http://location.to/myfile.php?type=daily
Run this script from cron to pass the variable.
Limitations
Note that using $argv[1] assumes that the command-line call provides the necessary parameter. Always check if $argv contains enough variables.
The above is the detailed content of How Can I Pass Variables to PHP Scripts from the Command Line?. For more information, please follow other related articles on the PHP Chinese website!