Unable to pass command line arguments to PHP script

(Note: I’m new to PHP)

I am trying to pass command line arguments to a PHP script on a Linux web server. I do not have access to the command line directly but I can run cron jobs OK which is how this needs to work.

I can call PHP scripts from cron OK so that bit is OK but when I look at $argv I can’t see any arguments, I can’t even see anything in $argv[0] which should be the script name.

I’ve been on this frustratingly simple step for a few hours and nothing online has helped.

I should be able to call:
script.php A B C
and see “A” in $argv[1], and “B” in $argv[2] etc.

Any ideas?

Thanks
Alasdair

That certainly seems to be the case, though I’ve never tried it myself. First question is, is your script running? Second question is, is this bit of the documentation relevant?

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

Hi,
Thanks for your reply.
The script is definitely being executed and I have output going from the script to a logfile.
I did see that register_argc_argv thing when I was trying to find the reason yesterday but mistakenly thought it had been deprecated. I think this is set to “0” which I guess means that passing command line arguments is disabled.
As this is on a web server I’m not sure how I can change that.
Any idea why there be a need for this to be disabled?

Cheers
Alasdair

I don’t have a good test case but maybe

php -d register_argc_argv=1 cmd.php arg

// cmd.php
var_dump($argv);

// Also try
var_dump($_SERVER['argv']);

According to a comment in the ini file, it might be disabled due to performance reasons. Though that does not make much sense.

The Symfony Console component uses $_SERVER[‘argv’]. Might consider just using the component as well unless your script is trivial.

Hi ahundiak,

Thanks for the reply.
I think that option might be disabled, I am new to PHP (OK with C, C++, C#) so I wanted to knock something up quickly to test some code but I have now done that another way.

Cheers

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.