How can PHP be used to write and read various data records to a file?

To write and read various data records to a file using PHP, you can use functions like fopen(), fwrite(), and fread(). First, open the file in write mode to write data records to it, and then open the file in read mode to read the data records. You can use formats like CSV or JSON to store structured data in the file.

// Write data records to a file
$file = fopen("data.txt", "w");
$data = "John,Doe,30\nJane,Smith,25";
fwrite($file, $data);
fclose($file);

// Read data records from a file
$file = fopen("data.txt", "r");
while(!feof($file)) {
    $line = fgets($file);
    $record = explode(",", $line);
    echo "Name: $record[0] $record[1], Age: $record[2]\n";
}
fclose($file);