Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined variable argc PHP

Tags:

linux

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.

like image 816
Pol0nium Avatar asked May 07 '13 22:05

Pol0nium


People also ask

How do you declare an undefined variable in PHP?

Fix Notice: Undefined Variable by using isset() Function This notice occurs when you use any variable in your PHP code, which is not set. Solutions: To fix this type of error, you can define the variable as global and use the isset() function to check if the variable is set or not.

What is argc in PHP?

$argc — The number of arguments passed to script.

What is $argv in PHP?

Introduction. When a PHP script is run from command line, $argv superglobal array contains arguments passed to it. First element in array $argv[0] is always the name of script. This variable is not available if register_argc_argv directive in php. ini is disabled.

What is undefined in PHP?

There are two methods in PHP called $_POST and $_GET methods which are used to obtain the input from the user while using a form. While using forms in PHP, if there is any variable or constant with no values assigned to them, then an error is encountered called undefined index in a manner “Notice: Undefined index” .


1 Answers

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.

like image 53
Tasos Bitsios Avatar answered Oct 04 '22 11:10

Tasos Bitsios