PHP passing $_GET in the Linux command prompt

PHP passing $_GET in the Linux command prompt

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP passing $_GET in the Linux command prompt

When you run php -S localhost:8000 or execute a script via CLI, the environment behaves differently than it does when accessed through an HTTP server. A common scenario involves accessing a resource like http://localhost/index.php?a=1&b=2&c=3. In that context, the query parameters are parsed by the SAPI (Server API) and populated into the global $_GET superglobal array.

If you attempt to replicate this exact behavior in a pure CLI environment using php -e index.php, the $_GET array remains empty. This is by design; the CLI interpreter does not automatically assume that command-line arguments correspond to HTTP query parameters unless explicitly told otherwise. You might initially consider using flags like php -e index.php --a 1 --b 2 --c 3 to mimic this behavior. While intuitive, this approach relies on a specific SAPI implementation and is not guaranteed to work across all PHP setups.

To pass values into $_GET from the command line in a standardized way, you should utilize the special variable $_SERVER['QUERY_STRING']. This environment variable holds the raw query string portion of the URI. By setting this variable before invoking the script, you ensure that the SAPI logic responsible for parsing $_GET receives the correct input.

Here is how you can achieve this using a simple wrapper script or direct command-line invocation:

php -r 'putenv("QUERY_STRING=a=1&b=2&c=3"); include "index.php";'

Alternatively, if you prefer to set an environment variable that persists for the specific process, you can do so like this:

QUERY_STRING="a=1&b=2&c=3" php index.php

Both methods effectively populate $_GET within the script's context. When the PHP interpreter runs the script, it checks $_SERVER['QUERY_STRING'], and if present, it parses the string according to standard URL encoding rules, populating $_GET with keys like a, b, and c.

However, there are significant trade-offs to consider when adopting this pattern. The primary concern is security. Manually constructing query strings on the command line often bypasses the strict input validation that a web server middleware (like Laravel's Request class or Symfony's HttpFoundation) would normally enforce. If your application relies heavily on framework-level sanitization for user inputs, simulating a GET request via CLI can inadvertently expose vulnerabilities if not handled with extreme caution.

Furthermore, this technique is strictly for simulation purposes. It does not mean the command-line arguments themselves are treated as part of the URL path or that they should be used for critical logic flows that depend on authenticated sessions or secure contexts. The $_SERVER['QUERY_STRING'] approach is useful for unit testing scripts that require parameter injection without spinning up a full web server, but it must not replace proper HTTP request simulation tools like PHPUnit's HttpClient or Guzzle when writing integration tests.

In the context of modern PHP frameworks, this manual parsing is largely obsolete. If you are working with Laravel, for instance, and need to test a controller action from the CLI, using the built-in artisan command is the superior approach:

php artisan route:run index.php --a=1 --b=2

While this specific syntax depends on custom middleware setup or package integration, standard Laravel testing utilizes phpunit with mocked requests. Relying on raw $_SERVER manipulation should be a last resort for legacy scripts or specific CLI-only utilities where full framework bootstrapping is unnecessary overhead.

Ultimately, understanding how the SAPI interprets input sources allows you to bridge the gap between web and console environments effectively. By setting QUERY_STRING, you gain control over $_GET without relying on non-standard flag parsing that may not exist in your specific PHP build. Always weigh the simplicity of this method against the potential risks of bypassing standard request validation layers.

Our network