Сравнение функций для работы с внешними программами
+----------------+-----------------+----------------+----------------+
| Command | Displays Output | Can Get Output | Gets Exit Code |
+----------------+-----------------+----------------+----------------+
| system() | Yes (as text) | Last line only | Yes |
| passthru() | Yes (raw) | No | Yes |
| exec() | No | Yes (array) | Yes |
| shell_exec() | No | Yes (string) | No |
| backticks (``) | No | Yes (string) | No |
+----------------+-----------------+----------------+----------------+
exec — Выполнить внешнюю программу
passthru — Выполнить внешнюю программу и отобразить необработанный вывод
shell_exec — Выполнить команду через оболочку и вернуть вывод в виде строки
system — Выполнить внешнюю программу и отобразить вывод
escapeshellarg — Экранировать строку для того, чтобы она могла быть использована как аргумент командной строки
escapeshellcmd — Экранировать метасимволы командной строки
proc_close — Завершить процесс, открытый proc_open и вернуть код возврата этого процесса
proc_get_status — Получить информацию о процессе, открытом proc_open
proc_nice — Изменить приоритет текущего процесса
proc_open — Выполнить команду и открыть указатель на файл для ввода/вывода
proc_terminate — Уничтожить процесс, открытый при помощи функции proc_open
- "Displays Output" means it streams the output to the browser (or command line output if running from a command line).
- "Can Get Output" means you can get the output of the command and assign it to a PHP variable.
- The "exit code" is a special value returned by the command (also called the "return status"). Zero usually means it was successful, other values are usually error codes.
Other misc things to be aware of:
- The shell_exec() and the backticks operator do the same thing.
- There are also proc_open() and popen() which allow you to interactively read/write streams with an executing command.
- Add "2>&1" to the command string if you also want to capture/display error messages.
- Use escapeshellcmd() to escape command arguments that may contain problem characters.
- If passing an $output variable to exec() to store the output, if $output isn't empty, it will append the new output to it. So you may need to unset($output) first.



