swellinfo Posted December 19, 2006 Share Posted December 19, 2006 I'm wondering how or if you can pass a variable to a php script from the command line. So, I'd like to pass the dynamic variable $pre = "wna"; to the command line before I process my script. Any help here? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/31262-passing-variable-from-command-line/ Share on other sites More sharing options...
AV1611 Posted December 19, 2006 Share Posted December 19, 2006 not sure what you mean... you mean like exec('wna');? Quote Link to comment https://forums.phpfreaks.com/topic/31262-passing-variable-from-command-line/#findComment-144631 Share on other sites More sharing options...
swellinfo Posted December 19, 2006 Author Share Posted December 19, 2006 no, I'd like to define a variable ($pre) outside of the script. So, where I am executing my script from a linux command line: php myscript.phpIs there a way to pass a variable to the script from the command line, or do I just need to create another file, and do an include? Quote Link to comment https://forums.phpfreaks.com/topic/31262-passing-variable-from-command-line/#findComment-144635 Share on other sites More sharing options...
swellinfo Posted December 19, 2006 Author Share Posted December 19, 2006 I figured this out: Passing variables on the command line is different than from a url, where you would typehttp://www.domain.com/myscript?var=whateverinstead, on the command line, you typephp myscript.php whatever you put a space in between the script and the value you are passing, and that value is available in the $argv variable. $argv is an array, and the first value is always the name of the script, and in this example case the second value would be "whatever". - $argv[1] = "whatever". Quote Link to comment https://forums.phpfreaks.com/topic/31262-passing-variable-from-command-line/#findComment-144647 Share on other sites More sharing options...
Daniel0 Posted December 19, 2006 Share Posted December 19, 2006 More info here: http://php.net/features.commandline Quote Link to comment https://forums.phpfreaks.com/topic/31262-passing-variable-from-command-line/#findComment-144655 Share on other sites More sharing options...
AV1611 Posted December 19, 2006 Share Posted December 19, 2006 replay I suppose you could also run another script that create a text file then parse the text file if you need to send more complicated stuff to the command line...Huh?Can PHP read an environment variable from the system? Quote Link to comment https://forums.phpfreaks.com/topic/31262-passing-variable-from-command-line/#findComment-144678 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.