PHP Shell_Exec() VS Exec()

PHP shell_exec() vs exec()

shell_exec returns all of the output stream as a string. exec returns the last line of the output by default, but can provide all output as an array specifed as the second parameter.

See

  • http://php.net/manual/en/function.shell-exec.php
  • http://php.net/manual/en/function.exec.php

What is different between exec(), shell_exec, system() and passthru() functions in PHP?

  • exec only returns the last line of the generated output.
  • shell_exec returns the full output of the command, when the command finished running.
  • system immediately shows all output, and is used to show text.
  • passthru also returns output immediately, but is used for binary data. passthru displays raw data.

With both exec and shell_exec it is possible to handle the output yourself, while system and passthru won't let you customize it and immediately display the output.

A more detailed comparison can be found here.

What are the differences of system(), exec() and shell_exec() in PHP?

exec — Execute an external program

system — Execute an external program and display the output

shell_exec — Execute command via shell and return the complete output as a string

so if you don't need the output, I would go with exec.

Further details:

  • http://php.net/manual/en/function.exec.php
  • http://php.net/manual/en/function.system.php
  • http://php.net/shell_exec

PHP system(), exec(), and shell_exec() not returning output

I think that the issue is not that you not get the output of the executed command, but which fails to find mysql.
Using exec you can get the return status of your command, where 0 means successful, and other values indicate an error.

$output = exec($cmd, $output, $retval);
var_dump($output);
var_dump($retval);

If the $retval is 1 that would mean which doesn't find the mysql binary, and returns an empty line.

Exec or Shell_exec: variables don't pass from php to the script

Doubles quotes should be at the beginning of the string.

'some string "$var" somestring' // won't interpolate
"some string '$var' somestring" // will interpolate

or you can concatenate them to the string

exec('../ems-scripts/mass-email-send.sh ' . $subject . ' ' . $body, $output)

You can read this discussion Should I use curly brackets or concatenate variables within strings? your problem is more about variable concatenation and interpolation, than really about exec() and shell_exec()



Related Topics



Leave a reply



Submit