In a multi-user environment, how can potential conflicts be avoided when multiple users are writing to the same log file for debugging purposes in PHP?

To avoid potential conflicts when multiple users are writing to the same log file in PHP, one solution is to use file locking. By implementing file locking, only one user can write to the log file at a time, preventing conflicts. This can be achieved using the `flock()` function in PHP.

$logFile = 'debug.log';
$logMessage = 'Debug message here';

$fp = fopen($logFile, 'a');
if (flock($fp, LOCK_EX)) {
    fwrite($fp, $logMessage . PHP_EOL);
    flock($fp, LOCK_UN);
} else {
    echo 'Could not lock file!';
}
fclose($fp);