Heim  >  Artikel  >  php教程  >  PHP中exec与system用法区别分析

PHP中exec与system用法区别分析

WBOY
WBOYOriginal
2016-06-06 20:19:021800Durchsuche

这篇文章主要介绍了PHP中exec与system用法区别分析,有助于深入掌握PHP程序设计,需要的朋友可以参考下

本文实例讲述了PHP中exec与system用法区别,分享给大家供大家参考之用。具体方法如下:

一般来说,在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);

希望本文所述对大家PHP程序设计的学习有所帮助。

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