php 中调用外部函数的方法有 4 种:使用 exec() 函数使用 system() 函数使用 passthru() 函数使用 proc_open() 函数

如何从 PHP 调用外部函数?
在 PHP 中,您可以通过不同的方法调用外部函数,这些方法包括:
1. 使用 exec() 函数:
立即学习“PHP免费学习笔记(深入)”;
$result = exec("ls -la");
echo $result;2. 使用 system() 函数:
system("ls -la");3. 使用 passthru() 函数:
passthru("ls -la");4. 使用 proc_open() 函数:
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w"),
);
$process = proc_open("ls -la", $descriptorspec, $pipes);
if (is_resource($process)) {
while ($line = fgets($pipes[1])) {
echo $line;
}
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
}实战案例:
以下是一个使用 exec() 函数从 PHP 调用外部命令 ls -la 的示例:
<?php
$output = exec("ls -la");
echo $output;
?>运行此代码将输出当前目录的文件和目录列表。











