PHP error guide
PHP CLI argc or argv is missing: meaning and fix
Error summary
Run the script under CLI, accept arguments through $argv or getopt, and account for register_argc_argv if code reads them outside normal CLI use.
What it means
Run the script under CLI, accept arguments through $argv or getopt, and account for register_argc_argv if code reads them outside normal CLI use.
What the error means
$argc and $argv are CLI request variables; they may be unavailable in non-CLI SAPIs or when configuration disables their registration.
Why PHP produces it
CLI-oriented code is executed through a web SAPI or assumes global argument variables without checking the runtime context.
PHP version notes
The behavior described for PHP CLI argc or argv is missing applies to PHP 7.4–8.4 unless a narrower version is stated; exact wording can vary by SAPI and patch release.
Most common causes
- A bin script is requested through the web server.
- register_argc_argv is disabled in a context that relies on it.
- A function expects $argv without importing or passing the global.
Minimal examples
BAD — reproduces the problem
$file = $argv[1];
FIXED — safer pattern
if (PHP_SAPI !== "cli") { exit(1); } $options = getopt("", ["file:"]);
Step-by-step diagnosis
- Check PHP_SAPI and PHP_BINARY at process entry.
- Inspect ini_get("register_argc_argv").
- Pass arguments into functions instead of reading implicit globals.
Fixes
Make the CLI contract explicit
Reject non-CLI execution and parse the provided argument array.
if (PHP_SAPI !== "cli") {
throw new RuntimeException("CLI only");
}
$options = getopt("", ["file:"]);
Common mistakes when fixing it
- Enabling register_argc_argv globally for a single web script.
- Reading positional offsets without validating count.
- Allowing a maintenance command to execute over HTTP.
How to prevent it
- Guard CLI entry points with PHP_SAPI.
- Use getopt or a command framework.
- Test zero, missing, and malformed arguments.
Web server / environment notes
cli, fpm, apache. Argument globals are intended for command-line execution.
Tags: cli,fpm,apache