Home  >  Article  >  Backend Development  >  How to call external functions from PHP?

How to call external functions from PHP?

WBOY
WBOYOriginal
2024-04-11 09:54:02415browse

There are 4 ways to call external functions in PHP: use the exec() function, use the system() function, use the passthru() function, use the proc_open() function

如何从 PHP 调用外部函数?

How to call external functions from PHP?

In PHP, you can call external functions through different methods, including:

1. Use the exec() function:

$result = exec("ls -la");
echo $result;

2. Use system() Function:

system("ls -la");

3. Use passthru() Function:

passthru("ls -la");

4. Use proc_open() Function:

$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);
}

Actual case:

The following is an example of using the exec() function to call an external command ls -la from PHP:

<?php
$output = exec("ls -la");
echo $output;
?>

Running this code will output the current List of files and directories for a directory.

The above is the detailed content of How to call external functions from PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn