I want to write text to a file (like log-entries/data/whatever), where the server is on a unix system
is it quicker to use:
$handle = fopen("somefile"); 
fwrite($handle,"sometext");
or this:
shell_exec("echo 'sometext' > somefile");
is there any other drawbacks to using the shell_exec method? speed? security? preformance?
For something as simple as your example, neither will be noticeably faster and security is no issue.  If your shell arguments are to be PHP variables, then be sure to use escapeshellcmd() on them (docs here).
However, in procedural code, I think I would prefer the fopen() fwrite() method, just because it's easier to check for errors and the validity of your file handle before writing to it, and then check the return of fwrite() to make sure your write operation succeeded.  The shell command would only return a single error code, so it would be more difficult to debug where the error occurred.
Way faster. Just consider that shell_exec is forking, then loading a shell binary, running it, then the shell interprets the command, then the shell forks again to run the command ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With