略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: 执行运算符

2024-12-23

执行运算符

PHP 支持一个执行运算符:反引号(``)。注意这不是单引号!PHP 将尝试将反引号中的内容作为 shell 命令来执行,并将其输出信息返回(即,可以赋给一个变量而不是简单地丢弃到标准输出)。使用反引号运算符“`”的效果与函数 shell_exec() 相同。

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

注意:

关闭了 shell_exec() 时反引号运算符是无效的。

注意:

与其它某些语言不同,反引号不能在双引号字符串中使用。

add a noteadd a note

User Contributed Notes 2 notes

up
129
robert
16 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
85
ohcc at 163 dot com
5 years ago
You can use variables within a pair of backticks (``).

<?php
    $host
= 'www.wuxiancheng.cn';
    echo `
ping -n 3 {$host}`;
?>

官方地址:https://www.php.net/manual/en/language.operators.execution.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3