Note that there are some explanatory texts on larger screens.

plurals
  1. POBizarre behavior of exec() in PHP
    primarykey
    data
    text
    <p>I'm still a newbie in PHP, so forgive me if I'm missing something obvious here.</p> <p>So, I'm calling some back-end scripts which produce lines of text, and I thought I'd tinker around with <code>exec</code> to make sure it works correctly. Of course, in the (Linux) shell, an easy multiline output command is something like </p> <pre><code>echo a;echo b;echo c </code></pre> <p>which, of course, produces what one would expect:</p> <pre><code>a b c </code></pre> <p>So, why am I getting an extra "c" when I run <code>exec</code> below?</p> <p>input:</p> <pre><code>exec("echo a;echo b;echo c",$output,$return); echo("return: $return\n"); echo("count: ". count($output) . "\n"); foreach($output as $i) {print "$i\n";} var_dump($output); </code></pre> <p>output:</p> <pre><code>return: 0 count: 4 a b c c array(4) { [0]=&gt; string(1) "a" [1]=&gt; string(1) "b" [2]=&gt; string(1) "c" [3]=&gt; string(1) "c" } </code></pre> <p>Now, I know that <code>exec</code> returns the last line processed, but I didn't echo the return value of <code>exec</code>. And what makes this even more puzzling is that if I make the strings a bit more "random", it seems to work correctly:</p> <pre><code>jmaney&gt; php -r 'exec("echo asfd;echo asdfasf;echo grrrr",$output,$return);echo("return: $return\n");echo("count: ". count($output) . "\n");foreach($output as $i){print "$i\n";}var_dump($output);' return: 0 count: 3 asfd asdfasf grrrr array(3) { [0]=&gt; string(4) "asfd" [1]=&gt; string(7) "asdfasf" [2]=&gt; string(5) "grrrr" } </code></pre> <p>What am I missing here?</p> <p><strong>Edited to add:</strong> I'm running bash shell version 3.2.48, SUSE Enterprise Linux version 11, Linux Kernel version 2.6, and PHP version 5.2.12. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload