Heim  >  Artikel  >  Backend-Entwicklung  >  PHP:exec与system区别

PHP:exec与system区别

WBOY
WBOYOriginal
2016-06-23 13:49:011057Durchsuche

在PHP中调用外部命令,可以用exec 及 system来实现: 

system() 
原型:string system (string command [, int return_var]) 
system()函数很其它语言中的差不多,它执行给定的命令,输出和返回结果。第二个参数是可选的,用来得到命令执行后的状态码。

    返回结果

        成功返回0,   
        失败(命令不存在等原因)   返回   非0值

exec() 
原型:string exec (string command [, string array [, int return_var]]) 
exec ()函数与system()类似,也执行给定的命令,但不输出结果,而是返回结果的最后一行。虽然它只返回命令结果的最后一行,但用第二个参数array 可以得到完整的结果,方法是把结果逐行追加到array的结尾处。所以如果array不是空的,在调用之前最好用unset()最它清掉。只有指定了第二 个参数时,才可以用第三个参数,用来取得命令执行的状态码。 
例子: 
exec("/bin/ls -l"); 
exec("/bin/ls -l", $res); 
exec("/bin/ls -l", $res, $rc); 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn