How to ignore errors in shell_exec?

I get shell command output in PHP as

$str = shell_exec("command");

and run the PHP script in the terminal. When it shell commandreturns an error, it will be printed on the terminal. How can I say shell_execto return command output only without error output?

+4
source share
3 answers

You just need to discard the error output by redirecting stderrto/dev/null

$str = shell_exec("command 2>/dev/null");

Exit without errors - stdout- will be saved in $str, as before.


, shell_exec @ try - catch, shell_exec ( PHP).

, , , .

some-command > /dev/null 2>&1, , ( ), , .


A : catch/discard stdout / stderr.

, , , , stdout stderr. (, stdout), .

+3

, :

some-command > /dev/null 2>&1
0

:

ini_set( 'display_errors', 0 );
error_reporting( E_ALL );

0, , 1,

-1

Source: https://habr.com/ru/post/1677991/


All Articles