php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48905 calling a php script via exec gives 0 as return code not respecting exit.
Submitted: 2009-07-13 14:02 UTC Modified: 2009-07-13 19:01 UTC
From: lars dot a dot johansson at se dot atlascopco dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3.0 OS: Linux
Private report: No CVE-ID: None
 [2009-07-13 14:02 UTC] lars dot a dot johansson at se dot atlascopco dot com
Description:
------------
Calling a php bug1B.php via exec gives invalid 0 as return code, when 
picking up 1 from caller when caller is PHP script. You call bug1A.php 
from a terminal. - ./bug1A.php

Script bugA1.php invokes bug1B.php via exec() with parameter '1', which 
script bug1B.php picks up. It looks like all digits are converted to 0 
(zero) if you do not explicit cast $argv[1] to integer.

When you call bug1B from a shell script it works like expected, 
returning 1 as return code.



Reproduce code:
---------------
#!/home/tooljn/PHP5.3/local/bin/php
<?php
//this is the caller - bug1A.php	
$lastOutputLine = exec("./bug1B.php 1", $output, $sysRC);
print "return code from bug1B.php rc=$sysRC\n";
?>

#!/home/tooljn/PHP5.3/local/bin/php
<?php
//this is the called - bug1B.php
// If we take away cast (int) string "1" is converted to integer 0 (zero), when called from bug1A.php
//if ($argc == 2 and ($argv[1] == '0' or $argv[1] == '1')) $rc = (int) $argv[1];
if ($argc == 2 and ($argv[1] == '0' or $argv[1] == '1')) $rc = $argv[1];
print "bug1B.php rc=$rc\n";
exit($rc);
?>

Expected result:
----------------
return code from bug1B.php rc=1;



Actual result:
--------------
return code from bug1B.php rc=0;

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-13 17:44 UTC] sjoerd-php at linuxonly dot nl
Thank you for your report.

exit() can take either a string or an integer as first parameter. An integer is used as exit code. A string, even if it is numeric, is output on standard out just before exiting. Thus exit(1) will exit with exit code 1, whereas exit("1") will print "1" and exit with error code 0.

Because parameters in $argv are strings, you pass a string to exit(), which is printed. You can verify this by printing $output in bugA1.php.
 [2009-07-13 19:01 UTC] jani@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 22 19:00:02 2025 UTC