Created
April 18, 2012 19:57
-
-
Save andreydjason/2416133 to your computer and use it in GitHub Desktop.
How to set variables args with command line in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is just an example, you may also look for this: https://github.com/jlogsdon/php-cli-tools