I have the following code but I’m trying to shorten it about one or two lines, as I believe the if is unnecessary. Is there any way the code below can shortened to a singular line?
if(file_exists($myFile))
{
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
}
else
{
$fh = fopen($myFile, 'w');
fwrite($fh, $message."\n");
}
==
==
== (because
achecks if the file exists and creates it if not)==
…of course,
file_put_contents()is only better if it is the only write you perform on a given handle. If you have any later calls tofwrite()on the same file handle, you’re better going with @Pekka’s answer.