I am calling a PHP script whenever a webpage loads. However, there is a parameter that the PHP script needs to run (which I normally pass through the command line when I am testing the script).
How can I pass this argument every time the script is run when the page loads?
Presumably you’re passing the arguments in on the command line as follows:
… and then accessing them in the script thusly:
What you need to be doing when passing arguments through HTTP (accessing the script over the web) is using the query string and access them through the $_GET superglobal:
Go to
http://yourdomain.example/path/to/script.php?argument1=arg1&argument2=arg2… and access:
If you want the script to run regardless of where you call it from (command line or from the browser) you’ll want something like the following:
as pointed out by Cthulhu in the comments, the most direct way to test which environment you’re executing in is to use the PHP_SAPI constant. I’ve updated the code accordingly: