calling exec on a php file and passing parameters?

calling exec on a php file and passing parameters?

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

Stefan Bogdanescu

Founder & Senior Architect

Understanding Parameter Passing in PHP Exec Calls

When you invoke the exec() function in PHP, you are essentially spawning a shell process to execute a command string. If you attempt to pass a variable directly into that string without proper handling, the operation fails because the shell interprets special characters within your data as commands or breaks the syntax of your argument list. The core issue isn't with exec() itself, but rather how the operating system's shell parses unescaped input before passing it to the PHP interpreter.

Consider a scenario where you have an ID stored in a variable: $id = '123'. If you construct your command like this:
$command = "php /var/www/unity/src/emailer.php?id=" . $id;
and then pass it to exec($command), the shell might interpret single quotes, double quotes, or other characters within $id as part of the shell syntax rather than literal data. More critically, if $id contains spaces or special characters like &, |, or ;, the shell will attempt to execute multiple commands or alter the argument structure entirely. This results in a silent failure where the script runs but produces no output, or it throws a fatal error if the syntax becomes invalid.

The most robust solution involves separating the command and its arguments so that PHP handles the escaping automatically. Instead of passing a single concatenated string to exec(), you can pass an array of arguments. When you provide an array, PHP treats each element as a distinct argument to the underlying shell or binary, effectively bypassing the need for manual string escaping.

Here is how you should structure the call:

$id = '123'; // Assume this comes from user input or a database

// Pass arguments as an array
exec('php /var/www/unity/src/emailer.php', $output, $return_var);
// Then append the query string manually if the target script expects URL params:
$command = "php /var/www/unity/src/emailer.php";
$arguments = ['?id=' => escapeshellarg($id)];

// However, a cleaner approach for CLI scripts is often:
$args = ["/var/www/unity/src/emailer.php", "--id=" . escapeshellarg($id)];
exec('php', $args, $return_var);
echo implode(' ', $args) . "\n";

The critical function here is escapeshellarg(). This built-in PHP function wraps strings in single quotes and escapes any internal single quotes, ensuring that the shell treats the content as a literal string rather than executable code. By using this function, you prevent command injection vulnerabilities and ensure that spaces within your ID (if they ever exist) do not break the argument parsing.

Another approach, particularly if the target PHP script is designed to handle parameters via the $_GET superglobal or specific CLI arguments, is to invoke the file directly without the explicit php executable prefix, provided you have execute permissions on the file itself. In this case:

$id = '123';
$cmd = "/var/www/unity/src/emailer.php?id=" . escapeshellarg($id);
exec($cmd, $output, $return_var);

The trade-off here is that relying on the file's shebang (#!/usr/bin/env php) assumes the PHP binary path is correctly configured in the web server environment. If you are running this from a CLI context (like Cron or a command line), using the explicit php executable is often safer and more portable across different environments.

Ultimately, the failure you experienced stems from trusting the shell to parse unescaped variables. By leveraging escapeshellarg() and structuring your commands as arrays when possible, you ensure that parameters are passed correctly regardless of their content. This pattern is essential for any production environment where user input influences command execution. For developers building complex CLI workflows within a Laravel application, this same principle applies to artisan commands or external tool integration; always escape arguments before injecting them into shell commands to maintain security and reliability.

Our network