0

Just interested what the you pros think as I came across the execution operator not so long ago and thought it was neater and cleaner to see/use but is there difference between using the execution operator or the build-in php function like exec() or system() as never seen it used before?

//execution operator
<?php 
    echo '<pre>'; 
    echo `netstat -a`;
    echo '</pre>'
?>



<?php 
echo '<pre>'; 
echo  system('netstat -a');
echo '</pre>'
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106

3 Answers3

1

The backtick operator is identical to shell_exec. There are some differences between the different system/exec/shell_exec/passthru etc. commands, please refer to the manual for details. If what you're looking for is the behavior of shell_exec, the backtick operator is just fine.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

It depends entirely on what it's being used for.

If it's just a quick shell out to another program and you just need what it printed to stdout, then backticks are fine. @deceze's post describes the details on how that works.

But if you need a more powerful level of interaction with the other program, then nothing beats proc_open. It lets you wield the full power of streams when working with the program's stdin, stdout and stderr.

Charles
  • 50,943
  • 13
  • 104
  • 142
  • yeah coz i made a simple remote command script when playing around with it and wondered why people would use complicated pipes and streams ect, so for a simple command that output the results the back to the browser back ticks will suffice. thanks – Lawrence Cherone Mar 18 '11 at 04:52
1

exec() in PHP is a bit of a misnomer. The actual syscall would terminate the running program and replace it by executing something else. But PHP does the forking behind the scenes, so that it works almost identically to system().

A difference in behaviour is that system does not buffer the results. But you will seldomly notice that from within PHP, as you'll get the completed results anyway. It's mostly significant for CLI applications only.

For the lower-level C background: Difference between "system" and "exec" in Linux?

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291