Undefined php argc variable

I am trying to execute this code:

function main(){ if ($argc < 1){ listDir("."); } else{ for($i = 0; $i <= sizeof($argv); $i++){ listDir($argv[$i]); } } } 

But I get the following error:

 PHP Notice: Undefined variable: argc in /home/me/test.php on line 15 

I thought $ argv and $ argc are global variables! How can I get rid of this error? Thanks.

+7
source share
3 answers

add

 global $argc, $argv; 

after

 function main() { 

These variables are in the global scope, but not in the scope of your function. The global keyword imports them.

+18
source

you probably want to do

 $argc = $_SERVER['argc']; $argv = $_SERVER['argv']; 
+3
source

I had the same problem.

You can run the following command.

 $ php -i | grep argv #run this comand 

Will output this:

 <tr><td class="e">register_argc_argv</td><td class="v"> Off</td><td class="v"> Off</td></tr> 

this means your php does not register argv and argc

If off, you can go to php.ini and set it as register_argc_argv = On .

This solved my problem.

+3
source

All Articles