Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Operators

The PHP backtick (execution) operator

Reference for the PHP backtick operator: it runs a shell command like shell_exec(), is deprecated in PHP 8.5, why it is dangerous with user input and safer options.

Backticks around a string run it as a shell command and return the command's standard output as a string, or null if the command fails or produces no output. It is exactly equivalent to calling shell_exec().

The operator is deprecated as of PHP 8.5: use shell_exec() instead. It is also easy to misuse. Any user-controlled text that reaches a shell can run arbitrary commands on your server, so use shell commands rarely, only with fixed commands, and escape every variable. In many hosting setups they are disabled completely.

PHP
$output = `ls -l`;                 // deprecated in PHP 8.5
$output = shell_exec("ls -l");     // the same, preferred

Try it

PHP Running a fixed command

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Escaping a variable

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
`command`
`command $variable`   // variables are interpolated like in double quotes

Return value: the output as string, or null when the command printed nothing or could not be run. The exit status is not available: use exec() or proc_open() when you need it.

Errors: if shell execution is disabled (disable_functions), PHP raises a warning and the result is null. PHP 8.5 emits a deprecation notice for the backtick syntax itself, as the first example shows.

Usage notes

  • Never put raw user input into a command. In the second example the semicolon starts a second command. escapeshellarg() quotes one argument, and escapeshellcmd() escapes a whole command (less reliable). Better still, pass an array to proc_open() so no shell is involved.
  • Only standard output is captured. Redirect with 2>&1 to include error output.
  • Same as shell_exec(). The function form can be passed around, checked and searched for in code reviews; that is one reason the backtick form is being retired.
  • Platform-specific. Commands differ between Linux and Windows, so scripts that depend on them are not portable.
  • Often disabled. Shared hosts and containers commonly list shell_exec, exec and proc_open in disable_functions. Prefer native PHP functions (for example scandir() instead of ls).
  • Only code, not strings. Inside quotes the backtick is a plain character; it acts as an operator only in normal code.