搜索
首页后端开发php教程php的exec在linux中返回值不能为负数的问题_PHP教程

php的exec在linux中返回值不能为负数的问题_PHP教程

Jul 13, 2016 am 10:56 AM
execlinuxphpwindows函数环境负数返回问题

exec函数在windows环境下是没有任何问题的,但在linux中返回值不能为负数。

string exec ( string $command [, array &$output [, int &$return_var ]] )

第三个参数, 怎么不能接收负数??
这里的&$return_var就是程序返回值,起初我的回答是可以为负数。
一般在C语言里我们会这样写

 代码如下 复制代码

#include
#include
int main()
{
    printf("^_^n");
    return -5;
}

这个-5就是返回值,但习惯上是写成0或者1的。
注意:很多人的C代码里把main函数写成 void main() 这样实际上是不对的,详细的就不说了。
把上面的代码编译后,到CMD下运行,然后就能看到输出结果了。接着,输入“echo %ERRORLEVEL%”,回车,就可以看到程序的返回值了。这个%ERRORLEVEL%就代表了程序的返回状态。在WIN下确实是可以为负数的。如图所示:

,php调用也是正常的。

 代码如下 复制代码
E:devphp535>php -r "exec('return.exe',$out,$a);var_dump($a);"
int(-2)

但是到了linux下,始终为正数,刚开始怀疑是权限问题,用了chmod +x后,排除了权限问题。

 代码如下 复制代码
exec("/home/wwwroot/test/rtest.out 2>&1",$out,$a);
var_dump($out,$a);
array(1) { [0]=> string(3) "^_^" } int(251)

看起来成了256+return val,可以看到实际上返回了负数,只不过被转换成正数了。
接着看了下standard/exec.c里的源代码,没发现啥端倪,干到很奇怪,突然想到自己忘了一步。忘了看程序返回给OS的值了.
可以使用echo $? 显示最后命令的推出状况。

 代码如下 复制代码
-bash-3.00$ vi main.c
-bash-3.00$ gcc -o ./mm main.c
-bash-3.00$ ll
total 48
drwxr-xr-x  3 www www 4096 May  4  2011 2011
drwxr-xr-x  6 www www 4096 Jun 23  2011 eoc
-rwxr-xr-x  1 www www 7131 Feb  1 12:47 hello
-rw-r--r--  1 www www    3 Feb  1 12:51 hello.c
-rw-r--r--  1 www www   99 Feb  1 12:50 main.c
-rwxr-xr-x  1 www www 4714 Feb  1 12:51 mm
drwxr-xr-x  3 www www 4096 Jun 24  2011 test
-bash-3.00$ ./mm
^_^
-bash-3.00$ echo $?
251
-bash-3.00$

这样就可以看看exec返换给OS的值是多少。
在linux下,这个返回值就是无符号类型,返回的是一个正数,所以传给php也是正数了,php实际上也是调用的exec所返回的值。

exec目录操作

 
2down vote  For greater control over how the child process will be executed, you can use the proc_open() function:

2 down vote

For greater control over how the child process will be executed, you can use the proc_open() function:

 代码如下 复制代码

$cmd  = 'Scripts/script.sh'; 
$cwd  = 'Scripts'; 
 
$spec = array( 
    // can something more portable be passed here instead of /dev/null? 
    0 => array('file', '/dev/null', 'r'), 
    1 => array('file', '/dev/null', 'w'), 
    2 => array('file', '/dev/null', 'w'), 
); 
 
$ph = proc_open($cmd, $spec, $pipes, $cwd); 
if ($ph === FALSE) { 
    // open error 

 
// If we are not passing /dev/null like above, we should close 
// our ends of any pipes to signal that we're done. Otherwise 
// the call to proc_close below may block indefinitely. 
foreach ($pipes as $pipe) { 
    @fclose($pipe); 

 
// will wait for the process to terminate 
$exit_code = proc_close($ph); 
if ($exit_code !== 0) { 
    // child error 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632187.htmlTechArticleexec函数在windows环境下是没有任何问题的,但在linux中返回值不能为负数。 string exec ( string $command [, array $output [, int $return_var ]] ) 第三个参数...
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP电子邮件:分步发送指南PHP电子邮件:分步发送指南May 09, 2025 am 12:14 AM

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自动化notifications andMarketingCampaigns.1)设置设置yourphpenvironcormentswironmentswithaweberswithawebserverserverserverandphp,确保themailfunctionisenabled.2)useabasicscruct

如何通过PHP发送电子邮件:示例和代码如何通过PHP发送电子邮件:示例和代码May 09, 2025 am 12:13 AM

发送电子邮件的最佳方法是使用PHPMailer库。1)使用mail()函数简单但不可靠,可能导致邮件进入垃圾邮件或无法送达。2)PHPMailer提供更好的控制和可靠性,支持HTML邮件、附件和SMTP认证。3)确保正确配置SMTP设置并使用加密(如STARTTLS或SSL/TLS)以增强安全性。4)对于大量邮件,考虑使用邮件队列系统来优化性能。

高级PHP电子邮件:自定义标题和功能高级PHP电子邮件:自定义标题和功能May 09, 2025 am 12:13 AM

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP发送电子邮件的指南使用PHP和SMTP发送电子邮件的指南May 09, 2025 am 12:06 AM

使用PHP和SMTP发送邮件可以通过PHPMailer库实现。1)安装并配置PHPMailer,2)设置SMTP服务器细节,3)定义邮件内容,4)发送邮件并处理错误。使用此方法可以确保邮件的可靠性和安全性。

使用PHP发送电子邮件的最佳方法是什么?使用PHP发送电子邮件的最佳方法是什么?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

PHP中依赖注入的最佳实践PHP中依赖注入的最佳实践May 08, 2025 am 12:21 AM

使用依赖注入(DI)的原因是它促进了代码的松耦合、可测试性和可维护性。1)使用构造函数注入依赖,2)避免使用服务定位器,3)利用依赖注入容器管理依赖,4)通过注入依赖提高测试性,5)避免过度注入依赖,6)考虑DI对性能的影响。

PHP性能调整技巧和技巧PHP性能调整技巧和技巧May 08, 2025 am 12:20 AM

phperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovesponsemetimes.2)优化

PHP电子邮件安全性:发送电子邮件的最佳实践PHP电子邮件安全性:发送电子邮件的最佳实践May 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具