Operator zur Programmausführung

PHP unterstützt einen Operator für die Ausführung externer Programme: Die sog. Backticks (``). Achtung: Die Backticks sind keine einfachen Anführungszeichen! PHP versucht, den Text zwischen den Backticks als Kommandozeilen-Befehl auszuführen. Die Ausgabe des aufgerufenen Programms wird zurückgegeben (d. h. wird nicht einfach ausgegeben, sondern kann einer Variablen zugewiesen werden). Die Verwendung des Backtick-Operators ist mit shell_exec() identisch.

<?php
$output
= `ls -al`;
echo
"<pre>$output</pre>";
?>

Hinweis:

Der Backtick-Operator steht nicht zur Verfügung, wenn die Funktion shell_exec() deaktiviert wurde.

Hinweis:

Anders als bei einigen anderen Sprachen haben Backticks keine besondere Bedeutung, wenn sie in Zeichenketten, die in doppelten Anführungszeichen eingeschlossen sind, verwendet werden.

add a note

User Contributed Notes 3 notes

up
141
robert
19 years ago
Just a general usage note.  I had a very difficult time solving a problem with my script, when I accidentally put one of these backticks at the beginning of a line, like so:[lots of code]`    $URL = "blah...";[more code]Since the backtick is right above the tab key, I probably just fat-fingered it while indenting the code.What made this so hard to find, was that PHP reported a parse error about 50 or so lines *below* the line containing the backtick.  (There were no other backticks anywhere in my code.)  And the error message was rather cryptic:Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /blah.php on line 446Just something to file away in case you're pulling your hair out trying to find an error that "isn't there."
up
98
ohcc at 163 dot com
8 years ago
You can use variables within a pair of backticks (``).<?php    $host = 'www.wuxiancheng.cn';    echo `ping -n 3 {$host}`;?>
up
0
paolo.bertani
2 years ago
If you want to avoid situations like the one described by @robert you may want to disable `shell_exec` and -as a consequence- the backtick operator.To do this just edit the `php.ini` file and add `shell_exec` to the `disable_functions` setting:    ; This directive allows you to disable certain functions.    ; It receives a comma-delimited list of function names.    ; https://php.net/disable-functions    disable_functions = "shell_exec"Then you can still use `exec()` to run terminal commands.
To Top