What are the advantages and disadvantages of using a database versus a text file for storing and sorting data in PHP?

When deciding between using a database or a text file for storing and sorting data in PHP, databases offer advantages such as better organization, faster retrieval, and built-in security features. However, databases can be more complex to set up and maintain compared to text files. Text files are simpler to work with but may not be as efficient for large amounts of data or complex queries.

// Example of storing data in a text file
$data = "Hello, World!";
$file = fopen("data.txt", "w");
fwrite($file, $data);
fclose($file);

// Example of retrieving data from a text file
$file = fopen("data.txt", "r");
$data = fread($file, filesize("data.txt"));
fclose($file);
echo $data;