Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loops with PHP shell_exec() : Use php for() or bash for-do-done?

Let's say I want to execute the command mycommand with PHP shell_exec() 10 times. Should I do a bash loop:

shell_exec('for i in {1 .. 10} do mycommand -i done');

or rather a PHP loop:

for($i = 1; $i <=10; ++$i) { shell_exec('mycommand -'.$i); }

What are the reasons (security, performance, style, ...) to choose one over the other?

like image 996
Foo Bar Avatar asked Aug 15 '26 06:08

Foo Bar


1 Answers

Go for bash loop, because shell_exec function is called only once. It will be faster than calling shell_exec multiple times. Enabling functions like exec, shell_exec itself a huge security issue. If someone managed to upload a PHP shell in your server then he can hack your server.

like image 117
Harikrishnan Avatar answered Aug 16 '26 18:08

Harikrishnan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!