실행 연산자

PHP는 하나의 실행 연산자를 지원합니다: 역따옴표(``). 홑따옴표가 아닌 점에 주의하십시오! PHP는 역따옴표 안의 내용을 쉘 명령으로 실행하려 할 것입니다; 출력을 반환합니다. (즉, 단순히 출력하는 것이 아닙니다; 변수에 할당할 수 있습니다) 역따옴표 연산자를 사용하는 것은 shell_exec()와 동일합니다.

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

Note:

역따옴표 연산자는 안전 모드가 켜져있거나 shell_exec()가 비활성화 되면 사용할 수 없습니다.

매뉴얼 프로그램 실행 함수 섹션, popen(), proc_open(), PHP를 명령줄에서 사용하기를 참고하십시오.

add a note add a note

User Contributed Notes 3 notes

up
141
robert
18 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 446

Just 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