Content · Programming · PHP

PHP exec() Function: Usage and Examples

Learn how PHP exec works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Executes a shell command and optionally collects output lines and its exit status.

What Is exec() in PHP?

Executes a shell command and optionally collects output lines and its exit status.

Its syntax is:

exec(string $command, array &$output = null, int &$result_code = null): string|false

Parameters:

  • $command — Accepted as string; required argument.
  • $output — Accepted as array &; optional default null.
  • $result_code — Accepted as int &; optional default null.

Return type: string|false. Never concatenate user input into shell commands. Avoid running shell programs where native PHP alternatives exist.

Availability: May be disabled by server policy; executes with the web process permissions.

Example 1: Basic Usage

<?php

echo function_exists("exec") ? "Available" : "Disabled";

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. Never concatenate user input into shell commands. Avoid running shell programs where native PHP alternatives exist.

Example 2: A Different Input

<?php

echo ini_get("disable_functions") ?: "No functions explicitly disabled";

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. Never concatenate user input into shell commands. Avoid running shell programs where native PHP alternatives exist.

Practical Example: Applying It in a Program

<?php

$allowedTask = "generate-local-report"; $allowedTasks = ["generate-local-report"]; if (in_array($allowedTask, $allowedTasks, true)) { echo "Run a fixed administrative command only in a controlled environment."; }

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. Never concatenate user input into shell commands. Avoid running shell programs where native PHP alternatives exist.

Important Behavior and Edge Cases

  • Key behavior: Never concatenate user input into shell commands. Avoid running shell programs where native PHP alternatives exist.
  • Environment: May be disabled by server policy; executes with the web process permissions.

Comparison With Related Functions

FunctionPurpose
exec()Executes a shell command and optionally collects output lines and its exit status.
proc_open()proc_open() gives more control over subprocess streams; exec() runs a command and collects its output lines.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: Never concatenate user input into shell commandsNever concatenate user input into shell commands. Avoid running shell programs where native PHP alternatives exist.

Key Takeaways

  • Executes a shell command and optionally collects output lines and its exit status.
  • Never concatenate user input into shell commands. Avoid running shell programs where native PHP alternatives exist.
  • May be disabled by server policy; executes with the web process permissions.

Summary: Never concatenate user input into shell commands. Avoid running shell programs where native PHP alternatives exist.

Share this resourceSend this page to someone who may find it useful.