Skip to content
Advertisement

Undefined variable argc PHP

I’m trying to execute this code:

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

But I’m getting the following error:

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

I thought that $argv and $argc were global variables. How can I get rid of this error?

I’m running this from command line.

Advertisement

Answer

Add a

global $argc, $argv;

after

function main() {

Those variables are in global scope, but not in your function’s scope. The global keyword imports them.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement