What are the advantages of using SQLite for counters in PHP over traditional file-based methods?

When using traditional file-based methods for counters in PHP, there is a risk of data corruption if multiple processes try to update the counter simultaneously. SQLite provides a more reliable and efficient solution as it handles concurrent access to the database. Additionally, SQLite offers better performance and scalability compared to file-based methods.

<?php
// Connect to SQLite database
$db = new SQLite3('counter.db');

// Create a table to store the counter
$db->exec('CREATE TABLE IF NOT EXISTS counter (count INTEGER)');

// Increment the counter
$db->exec('UPDATE counter SET count = count + 1');

// Retrieve and display the current count
$result = $db->querySingle('SELECT count FROM counter');
echo 'Counter: ' . $result;