How can PHP developers prevent sensitive information, such as passwords, from being stored in log files?

To prevent sensitive information like passwords from being stored in log files, PHP developers can use the `filter_var()` function to check if the log message contains any sensitive information before writing it to the log file. If sensitive information is found, it can be replaced with placeholder text before logging.

<?php
$logMessage = "User login with password: mySecretPassword123";
$filteredMessage = filter_var($logMessage, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/\b(?:password|secret)\b/i")));
if($filteredMessage !== false) {
    $logMessage = preg_replace("/\b(?:password|secret)\b/i", "*****", $logMessage);
}
file_put_contents('log.txt', $logMessage . PHP_EOL, FILE_APPEND);
?>