Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$argv not defined when executing PHP CLI script from Windows 10 XAMPP

I'm attempting to execute a PHP script from the command line in Windows 10 while using XAMPP for localhost.

From the Git terminal application on Windows 10, I'm running the following command:

/c/xampp/php/php.exe /path/to/script/script.php arg1=val1

The script executes correctly, but when I try to do the following:

print_r($argv);

I get the following error:

PHP Notice:  Undefined variable: argv in /path/to/script/script.php on line line-#.

Also, php_sapi_name() returns cli, so I should be good, but both $argv and $argc return as undefined.

Does anyone know why? Thanks.

like image 705
HartleySan Avatar asked Jun 15 '26 08:06

HartleySan


1 Answers

As PHP docs say, $argv and $argc variable are not defined unless such feature is explicitly enabled via php.ini's register_argc_argv, so check your config file first.

But instead of depending on runtime environment configurable I'd rather recommend keeping register_argc_argv option off and recommend your code to use $_SERVER['argv'] / $_SERVER['argc'] instead. This will work because the $_SERVER superglobal is populated in CLI mode too, and these keys are always present, regardless of state of register_argc_argv config.

like image 189
Marcin Orlowski Avatar answered Jun 17 '26 09:06

Marcin Orlowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!