How do I know when to use each of the execution functions?

Asked

Viewed 196 times

10

How each of the PHP functions works:

  1. pcntl_exec('/caminho/executar');
  2. exec('/caminho/executar');
  3. shell_exec('/caminho/executar');

Is there any singularity between the 3 examples cited above?

Although it is clear that the exec() is to execute program directly, the shell_exec() can also run a program via command line, and the pcntl_exec() which also runs program, but has something to do with process, which I didn’t understand right.

To PHP documentation did not direct me very well to understand these differences: pcntl_exec, exec and shell_exec.

  • 1

    Where is @Bacco when you need it!!!

1 answer

10


In addition to the 3 you mentioned, there is still passthru and the system for path execution.

  • pcntl_exec Creates a separate process from the main process with the executed command, yet allows to set arguments and other settings such as which user should perform this process (only if you have as root)it is developed for UNIX systems and cannot be used on webservers, only in php running via command line (CLI). It is very useful if you are developing an application that needs to manage workspaces and to which user delegate the process. The detail is that this function is part of the PCNTL extension: http://php.net/manual/en/book.pcntl.php
  • exec Only returns the last line generated by the output of the specified program execution.
  • shell_exec Returns all the output of the command when it has finished running.
  • system Runs immediately displaying output, is used to display text.
  • passthru Also returns output immediately but is used to display binary data. passthru displays the "raw data" directly to the browser.

With both commands exec andshell_exec it is possible to capture the output(or output), while the system and the passthru will not let you customize and display output immediately.

The pcntl_exec made it necessary when we need to create a block of code that runs asynchronously to the main request, compatible only when php is running in fast-cgi or php-fpm, apache_handler will not work. It allows you to make a "branch" where your code follows 2 paths based on an if, one within the "child" process and another in the "Father" process".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.