Mateusz Papiernik
Mateusz Papiernik

Reputation: 870

$GLOBALS["argv"] and $argv empty, $_SERVER["argv"] just fine

I am fighting with a (perhaps) simple problem with PHP CLI running on Hostgator shared hosting. A simple code:

<?php
var_dump($argv);
var_dump($GLOBALS["argv"]);
var_dump($_SERVER["argv"]);
var_dump(ini_get("register_argv_argv"));
?>

When run on my local machine as well as several other instances of php as:

php test.php arg1

Outputs correctly as predicted:

array (
  0 => 'test.php',
  1 => 'arg1'
)
array (
  0 => 'test.php',
  1 => 'arg1'
)
array (
  0 => 'test.php',
  1 => 'arg1'
)
1

But run from cron on Hostgator shared hosting it outputs:

array (
)
NULL
array (
  0 => 'test.php',
  1 => 'arg1'
)
1

The code I am fighting with is a legacy one and that relies heavily upon $GLOBALS["argv"] to parse command line parameters. I have no means to modify the whole codebase to rely on $_SERVER["argv"] instead.

What might be the reason why all my instances of PHP fills all the arrays with commandline parameters, and the hosted php on Hostgator does not populate $GLOBALS["argv"] at all?

I made a workaround to deal with this problem, but I hate when I don't know why something is happening :)

Thanks for any ideas!

Upvotes: 4

Views: 3900

Answers (1)

pbs
pbs

Reputation: 248

I had the same problem with no $_SERVER['argv'];

Try php-cli instead of php:

$job = "* * * * * php-cli /home/user/file.php surf";

it worked for me!

Upvotes: 6

Related Questions