What are the best practices for managing and updating visitor count data in a text file using PHP, considering scalability and efficiency?

To efficiently manage and update visitor count data in a text file using PHP, it is recommended to use file locking to prevent race conditions when multiple visitors access the file simultaneously. Additionally, using a separate function to read and update the visitor count can help improve scalability and maintainability of the code.

<?php
function updateVisitorCount($file) {
    $fp = fopen($file, 'r+');
    if (flock($fp, LOCK_EX)) {
        $count = intval(fread($fp, filesize($file)));
        $count++;
        ftruncate($fp, 0);
        fwrite($fp, $count);
        flock($fp, LOCK_UN);
    }
    fclose($fp);
}

$file = 'visitor_count.txt';
updateVisitorCount($file);
?>