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.
$output = `ls -l`; // deprecated in PHP 8.5 $output = shell_exec("ls -l"); // the same, preferred
Try it
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.
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
`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, andescapeshellcmd()escapes a whole command (less reliable). Better still, pass an array toproc_open()so no shell is involved. - Only standard output is captured. Redirect with
2>&1to 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,execandproc_openindisable_functions. Prefer native PHP functions (for examplescandir()instead ofls). - Only code, not strings. Inside quotes the backtick is a plain character; it acts as an operator only in normal code.