NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
### 1\. 使用`exec()` `exec()`函数用于执行一个外部程序。 ~~~php <?php $output = []; $return_var = 0; exec('php think', $output, $return_var); if ($return_var === 0) { echo "命令执行成功,输出如下:\n"; print_r($output); } else { echo "命令执行失败。\n"; } ?> ~~~ ### 2\. 使用`shell_exec()` `shell_exec()`函数执行通过 shell 环境的命令,并返回完整的输出作为字符串。 ~~~php //$output = shell_exec('php think'); $output = shell_exec('php D:\PRO\phpstudy_pro\WWW\www.xx.com\think test 1 -t 2'); if (!empty($output)) { echo "命令执行成功,输出如下:\n"; echo $output; } else { echo "命令执行失败或无输出。\n"; } ~~~ ### 3\. 使用`system()` `system()`函数执行外部程序,并且显示输出。 ~~~php $return_var = 0; system('php think', $return_var); if ($return_var === 0) { echo "命令执行成功。\n"; } else { echo "命令执行失败。\n"; } ~~~ ### 4\. 使用`passthru()` `passthru()`函数执行外部程序,并且直接将原始输出显示给浏览器或命令行。 ~~~php $return_var = 0; passthru('php think', $return_var); if ($return_var === 0) { echo "命令执行成功。\n"; } else { echo "命令执行失败。\n"; } ~~~ ``` // 检查是否有 php think test 进程 $processes = shell_exec('ps aux | grep "php think test"'); if (strpos($processes, 'php think test') !== false) { echo "php think test 命令正在执行\n"; } else { echo "php think test 命令没有在执行\n"; } ```