How to Pass Arguments to PHP in Command Line
Tags: cli, command line, cron, php, programming, wamp
I did a little bit of searching this morning for how to pass arguments to php via command line because I was working on setting up a php script to run on a cron in Windows (using WAMP).
It’s actually pretty easy. Say your command line looks like this:
>path-to-php/php.exe path-to-script/script.php
Passing an argument to the php script would look like this:
>path-to-php/php.exe path-to-script/script.php arg1 arg2
PHP will read the arguments using the $argv variable. So in our example, $argv[0] would be the name of the script (script.php), $argv[1] would be arg1 and $argv2[2] would be arg2.
Another example:
>path-to-php/php.exe path-to-script/script.php 15
Say you just wanted to echo out the argument contents:
echo $argv[1];
Pretty cool. Here’s the forum post where I discovered this information.
