Argv and argc are undefined or null

6.3k views Asked by At

When i try this in __construct:

var_dump($argc);
var_dump($argv);
var_dump($_SERVER["argv"]);

It returns error

Undefined variable: argc and
Undefined variable: argv

and array

(size=0) empty

When i declare $argc and $argv in global it returns all null.

Also i am parsing arguments using nncron like this:

* * * * * php \php\class.xmlcontroler.php timeout=60

0 * * * * php \php\class.xmlcontroler.php timeout=3600

What is solution?

2

There are 2 answers

1
joconja On

$argv and $argc are only available in the global namespace. You would have to handle them as a parameter to your constructor.

<?php
function foo()
{
    var_dump($argv);
}
echo "global\n";
var_dump($argv);

echo "function\n";
foo();

Will provide:

global
array(2) {
  [0]=>
  string(5) "a.php"
  [1]=>
  string(3) "123"
}
function
NULL

when called like this php a.php 123

Update: Class example

<?php
class Foo
{
    public function __construct($argv)
    {
        // use $argv
    }
}

$foo = new Foo($argv);
0
logi-kal On

From PHP: $argc - Manual:

Note: This variable is not available when register_argc_argv is disabled.

So you have to enable the register_argc_argv flag in your PHP configuration file.