Passing Variables to Command-Line PHP Scripts
When running a PHP script from the command line using crontab, you may encounter challenges in passing variables. The syntax you attempted with a query string (myfile.php?type=daily) is not supported in this context.
To resolve this issue, pass the variable as an argument to the PHP executable. Replace your command with:
php myfile.php daily
Within your PHP script, retrieve the variable from the $argv array:
$type = $argv[1]; // Assuming '$argv[0]' contains the script name
Alternative Approaches:
If the script is also used as a webpage, you have two options:
#!/bin/sh wget http://location.to/myfile.php?type=daily
if (defined('STDIN')) { $type = $argv[1]; } else { $type = $_GET['type']; }
Remember to ensure that the $argv array contains the necessary variables and handle edge cases as needed.
The above is the detailed content of How to Pass Variables to Command-Line PHP Scripts?. For more information, please follow other related articles on the PHP Chinese website!