Skip to content

Instantly share code, notes, and snippets.

@andreydjason
Created April 18, 2012 19:57
Show Gist options
  • Save andreydjason/2416133 to your computer and use it in GitHub Desktop.
Save andreydjason/2416133 to your computer and use it in GitHub Desktop.
How to set variables args with command line in PHP
#!/usr/bin/php <- location of the php parser
<?php
# This script receives command line args and transforms it into an array of *params
# example from command line: ($ <- is the bash)
# $ /usr/bin/env php script.php --onevar=ThisIsMyValue --another_var=1 --some-var="some value" --integerVar=102323
# in theory, the variables accepts any kind of value, just set the value as ypu set it in PHP
error_reporting(0);
$this_script = $_SERVER["SCRIPT_NAME"];
$argvs = array();
foreach ($argv as $arg_value) {
list($var_name, $var_value) = explode('=', $arg_value);
$var_name = str_replace('--', '', $var_name);
if ($var_name == $this_script) continue;
$argvs["$var_name"] = $var_value;
}
# accessing:
foreach($argvs as $key => $value) {
echo "$key => $value \n";
}
@andreydjason
Copy link
Author

This is just an example, you may also look for this: https://github.com/jlogsdon/php-cli-tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment