In what scenarios would using SQLite via PDO be a suitable option for managing data in PHP applications, and how does it compare to using text files?

SQLite via PDO would be a suitable option for managing data in PHP applications when you need a lightweight, serverless database solution that can easily handle small to medium-sized datasets. It provides a more structured and efficient way to store and retrieve data compared to using text files. Additionally, SQLite offers features like transactions, indexes, and query optimization, making it a more robust option for data management.

// Connect to SQLite database using PDO
$pdo = new PDO('sqlite:/path/to/database.sqlite');

// Create a table
$pdo->exec("CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY,
    username TEXT NOT NULL,
    email TEXT NOT NULL
)");

// Insert data into the table
$stmt = $pdo->prepare("INSERT INTO users (username, email) VALUES (:username, :email)");
$stmt->execute(['username' => 'john_doe', 'email' => 'john_doe@example.com']);

// Retrieve data from the table
$stmt = $pdo->query("SELECT * FROM users");
while ($row = $stmt->fetch()) {
    echo $row['username'] . ": " . $row['email'] . "\n";
}