<p>I'm trying to run PHP from the command line under <a href="https://en.wikipedia.org/wiki/Windows_XP" rel="nofollow noreferrer">Windows XP</a>.</p>
<p>That works, except for the fact that I am not able to provide parameters to my PHP script.</p>
<p>My test case:</p>
<pre><code>echo &quot;param = &quot; . $param . &quot;\n&quot;;
var_dump($argv);
</code></pre>
<p>I want to call this as:</p>
<pre><code>php.exe -f test.php -- param=test
</code></pre>
<p>But I never get the script to accept my parameter.</p>
<p>The result I get from the above script:</p>
<blockquote>
<p>PHP Notice:  Undefined variable: param in C:\test.php on line 2</p>
</blockquote>
<pre><code>param = ''
array(2) {
  [0]=&gt; string(8) &quot;test.php&quot;
  [1]=&gt; string(10) &quot;param=test&quot;
}
</code></pre>
<p>I am trying this using PHP 5.2.6. Is this a bug in PHP 5?</p>
<p>The parameter passing is handled in the <a href="http://us3.php.net/features.commandline" rel="nofollow noreferrer">online help</a>:</p>
<blockquote>
<p>Note: If you need to pass arguments to your scripts you need to pass -- as the first argument when using the -f switch.</p>
</blockquote>
<p>This seemed to be working under PHP 4, but not under PHP 5.</p>
<p>Under PHP 4 I could use the same script that could run on the server without alteration on the command line. This is handy for local debugging, for example, saving the output in a file, to be studied.</p>

[{'answer_id': 65233, 'author': 'Adam Wright', 'author_id': 1200, 'author_profile': 'https://Stackoverflow.com/users/1200', 'pm_score': 3, 'selected': False, 'text': "<p>Why do you have any expectation that <em>param</em> will be set to the value?</p>\n<p>You're responsible for parsing the command line in the fashion you desire, from the <em>$argv</em> array.</p>\n"}, {'answer_id': 65269, 'author': 'Ben', 'author_id': 5005, 'author_profile': 'https://Stackoverflow.com/users/5005', 'pm_score': 0, 'selected': False, 'text': '<p>$argv is an array containing all your commandline parameters... You need to parse that array and set $param yourself.</p>\n\n<pre><code>$tmp = $argv[1];             // $tmp="param=test"\n$tmp = explode("=", $tmp);   // $tmp=Array( 0 =&gt; param, 1 =&gt; test)\n\n$param = $tmp[1];            // $param = "test";\n</code></pre>\n'}, {'answer_id': 65299, 'author': 'Jeremy Weathers', 'author_id': 8794, 'author_profile': 'https://Stackoverflow.com/users/8794', 'pm_score': 1, 'selected': False, 'text': '<p>PHP does not parameterize your command line parameters for you. See the output where your second entry in <code>ARGV</code> is &quot;param=test&quot;.</p>\n<p>You most likely want to use the PEAR package <a href="http://pear.php.net/package/Console_CommandLine" rel="nofollow noreferrer">Console_CommandLine</a>: &quot;A full featured command line options and arguments parser&quot;.</p>\n<p>Or you can be masochistic and add code to go through your <code>ARGV</code> and set the parameters yourself. Here\'s a very simplistic snippet to get you started (this won\'t work if the first part isn\'t a valid variable name or there is more than 1 \'=\' in an <code>ARGV</code> part:</p>\n<pre><code>foreach($argv as $v) {\n    if(false !== strpos($v, \'=\')) {\n        $parts = explode(\'=\', $v);\n        ${$parts[0]} = $parts[1];\n    }\n}\n</code></pre>\n'}, {'answer_id': 65308, 'author': 'Derek Kurth', 'author_id': 1418, 'author_profile': 'https://Stackoverflow.com/users/1418', 'pm_score': 0, 'selected': False, 'text': "<p>You can do something like:</p>\n\n<pre><code>if($argc &gt; 1){\n    if($argv[1] == 'param=test'){\n        $param = 'test';\n    }\n}\n</code></pre>\n\n<p>Of course, you can get much more complicated than that as needed.</p>\n"}, {'answer_id': 65359, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n<p>The parameter passing is handled in the online help Note: If you need to pass arguments to your scripts you need to pass -- as the first argument when using the -f switch. This seemed to be working under PHP\xa04, but not under PHP\xa05.</p>\n</blockquote>\n<p>But PHP still doesn't parse those arguments.  It just passes them to the script in the <em>$argv</em> array.</p>\n<p>The only reason for the <strong>--</strong> is so that PHP can tell which arguments are meant for the PHP executable and which arguments are meant for your script.</p>\n<p>That lets you do things like this:</p>\n<pre><code>php -e -n -f myScript.php -- -f -n -e\n</code></pre>\n<p>(The -f, -n, and -e options <strong>after</strong> the <code>--</code> are passed to file <em>myScript.php</em>. The ones before are passed to PHP itself).</p>\n"}, {'answer_id': 65709, 'author': 'farzad', 'author_id': 9394, 'author_profile': 'https://Stackoverflow.com/users/9394', 'pm_score': -1, 'selected': False, 'text': "<p>You can use the $argv array. Like this:</p>\n<pre><code>&lt;?php\n    echo $argv[1];\n?&gt;\n</code></pre>\n<p>Remember that the first member of the <em>$argv</em> array (which is <em>$argv[0]</em>) is the name of the script itself, so in order to use the parameters for the application, you should start using members of the <em>$argv[]</em> from the '1'th index.</p>\n<p>When calling the application, use this syntax:</p>\n<pre><code>php myscript.php -- myValue\n</code></pre>\n<p>There isn't any need to put a name for the parameter. As you saw, what you called the <em>var_dump()</em> on the <em>$argv[]</em>, the second member (which is the first parameter) was the string <em>PARAM=TEST</em>. Right? So there isn't any need to put a name for the param. Just enter the <em>param</em> value.</p>\n"}, {'answer_id': 66690, 'author': 'rcphq', 'author_id': 9114, 'author_profile': 'https://Stackoverflow.com/users/9114', 'pm_score': 0, 'selected': False, 'text': '<p>You could use something like</p>\n<pre><code>if (isset($argv[1]) {\n  $arg1 = $argv[1];\n  $arg1 = explode(&quot;=&quot;, $arg1);\n  $param = $arg1[1];\n}\n</code></pre>\n<p>(How to handle the lack of parameters is up to you.)</p>\n<p>Or if you need a more complex scenario, look into a command-line parser library, such as the one from <a href="http://pear.php.net/package/Console_CommandLine" rel="nofollow noreferrer">Pear</a>.</p>\n<p>Using the <code>${$parts[0]} = $parts[1];</code> posted in another solution lets you override any variable in your code, which <strong>doesn’t really sound safe</strong>.</p>\n'}, {'answer_id': 1557173, 'author': 'kander', 'author_id': 151847, 'author_profile': 'https://Stackoverflow.com/users/151847', 'pm_score': 0, 'selected': False, 'text': '<p>If you like living on the cutting edge, PHP 5.3  has the <a href="http://php.net/getopt" rel="nofollow noreferrer">getOpt()</a> command which will take care of all this messy business for you. Somewhat.</p>\n'}, {'answer_id': 5978116, 'author': 'Chetan Sharma', 'author_id': 170445, 'author_profile': 'https://Stackoverflow.com/users/170445', 'pm_score': 2, 'selected': False, 'text': '<p>You can use the <a href="http://in2.php.net/getopt" rel="nofollow noreferrer">getopt()</a> function.</p>\n<p>Check blog post <em><a href="http://sharingphp.blogspot.com/2011/04/php-cli-script-and-command-line.html" rel="nofollow noreferrer">PHP CLI script and Command line arguments</a></em>.</p>\n'}, {'answer_id': 6156922, 'author': 'Gerrit', 'author_id': 773654, 'author_profile': 'https://Stackoverflow.com/users/773654', 'pm_score': 2, 'selected': False, 'text': '<p>If you want to pass the parameters similar to GET variables, then you can use the <a href="http://us.php.net/manual/en/function.parse-str.php" rel="nofollow noreferrer">parse_str()</a> function. Something similar to this:</p>\n<pre><code>&lt;?php\n    parse_str($argv[1]);\n?&gt;\n</code></pre>\n<p>Would produce a variable, <em>$test</em>, with a value of &lt;myValue&gt;.</p>\n'}, {'answer_id': 26269594, 'author': 'M.Hefny', 'author_id': 637718, 'author_profile': 'https://Stackoverflow.com/users/637718', 'pm_score': 1, 'selected': False, 'text': '<p>Command-line example:</p>\n<pre class="lang-none prettyprint-override"><code>    php myserver.php host=192.168.1.4 port=9000\n</code></pre>\n<p>In file <em>myserver.php</em>, use the following lines:</p>\n<pre><code>&lt;?php\n    parse_str(implode(\'&amp;\', array_slice($argv, 1)), $_GET);\n    // Read arguments\n\n    if (array_key_exists(\'host\', $_GET))\n    {\n        $host = $_GET[\'host\'];\n    }\n\n    if (array_key_exists(\'port\', $_GET))\n    {\n        $port = $_GET[\'port\'];\n    }\n?&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/65173', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']