Home > Article > Backend Development > How to call external functions from PHP?
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
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!