Home  >  Article  >  Backend Development  >  Introduction to the usage of php's system() function & exec() function & passthru() function

Introduction to the usage of php's system() function & exec() function & passthru() function

不言
不言Original
2018-08-17 17:15:066641browse

This article brings you an introduction to the usage of PHP's system() function & exec() function & passthru() function. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. helped.

Specific usage of functions

As a newbie like me, I don’t even know how to use php system, so I will make a simple record today.

system() outputs and returns the last line of shell results.
exec() does not output results and returns the last line of shell results. All results can be saved in a returned array.
passthru() only calls the command and outputs the command execution result directly to the standard output device as is.

<?php
echo &#39;<pre class="brush:php;toolbar:false">&#39;;

// 输出 shell 命令 "ls" 的返回结果
// 并且将输出的最后一样内容返回到 $last_line。
// 将命令的返回值保存到 $retval。
$last_line = system(&#39;ls&#39;, $retval);

// 打印更多信息
echo &#39;

Last line of the output: ' . $last_line . '
Return value: ' . $retval; ?>

The returned result is the result after executing the command in Linux (it turns out that this is the usage, but I am still too good)

<pre class="brush:php;toolbar:false">anaconda-post.log
bin
code
dev
entrypoint.sh
etc
home
lib
lib64
media
mnt
opt
proc
root
run
run.sh
sbin
srv
sys
tmp
usr
var

Last line of the output: var
Return value: 0 sandbox> exited with status 0

Related recommendations:

What are the common methods for operating redis in php? Summary of the method of operating redis in PHP (with code)

The complete code for PHP to implement bubble sorting and binary search


The above is the detailed content of Introduction to the usage of php's system() function & exec() function & passthru() function. 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