Shell run/execute php script with parameters

Shell run/execute php script with parameters

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

Stefan Bogdanescu

Founder & Senior Architect

Shell run/execute php script with parameters

There is a fundamental misunderstanding in how web servers and command-line interpreters handle data that often leads to confusion when trying to pass query string parameters via the CLI. The core issue lies in distinguishing between a URL query string and actual executable arguments. When you run php -q htdocs/file.php?show=show_name, the shell treats the entire segment after the script path, including the ? and the key-value pair, as part of the filename or an invalid argument rather than passing it to the PHP interpreter's input stream.

PHP scripts running on a web server interpret data following the ? symbol as part of the request URL, accessible via the $_GET superglobal array. However, when invoking PHP directly from the command line, the engine does not automatically parse arguments as if they were an HTTP request unless explicitly told to do so. The query string syntax is specific to the web context, not the CLI execution model.

To pass parameters correctly to a script via the shell, you must supply them as standard executable arguments. In the php -q htdocs/file.php example, the -q flag stands for quiet mode, which suppresses the startup message but does not alter how arguments are parsed. To pass a variable named show with the value show_name, you need to separate the script path from the argument using spaces.

The correct command structure is:

php -q htdocs/file.php show=show_name

Or, without the quiet flag for more verbose output during development:

php htdocs/file.php show=show_name

In this scenario, PHP treats show as a global variable available within the script's scope. If your script contains code like $show = 'default';, you would need to check if the argument exists first. A common pattern inside the script might look like this:

if (isset($show)) {
    echo "The value is: " . $show;
} else {
    echo "No parameter provided.";
}

This approach mimics the behavior of a GET request parameter, but strictly within the CLI environment. The key takeaway is that the shell passes everything after the script path as distinct arguments to the php executable, not as parts of a URL.

There are scenarios where you might want the actual query string format (e.g., ?show=show_name) to be processed by the script as if it were a web request. If your application logic relies heavily on $_GET, passing arguments directly like php file.php show=show_name will not populate $_GET. Instead, it populates the global $show variable. To bridge this gap without switching to direct global variables, you can use the -r option to run raw PHP code that simulates a request or use the php://input stream, though for simple parameter passing, global variables are usually sufficient and more efficient.

Another consideration involves security. When executing scripts from the command line, especially on production servers, always validate arguments before using them. Passing user input directly to a script path (command injection) is a significant risk. For example:

php file.php "$(echo $user_input)"

This constructs a dynamic argument list that can be exploited if $user_input contains malicious characters. Always whitelist allowed values or sanitize inputs rigorously before execution.

If you are migrating logic from a web environment to CLI automation, ensure your scripts handle missing parameters gracefully. Failing to check isset($param) before accessing it will result in undefined variable notices, which can clutter logs and obscure real errors. Using strict types (declare(strict_types=1);) combined with argument validation at the entry point of your script is a robust practice for CLI tools.

For more complex needs, such as running multiple scripts or passing arrays of parameters, you might need to construct the command string dynamically in your shell script or use eval cautiously, though direct argument passing remains the safest and most readable approach.

Our network