How can PHP's file functions be utilized to read and write data to a text file from a form submission?

To read and write data to a text file from a form submission using PHP, you can use the file functions like fopen, fwrite, and fclose. When a form is submitted, you can retrieve the data using $_POST or $_GET, then open the text file in write mode to write the data. To read the data from the text file, you can open the file in read mode and use functions like fgets or file_get_contents.

<?php
// Get form data
$data = $_POST['data'];

// Write data to text file
$file = fopen('data.txt', 'w');
fwrite($file, $data);
fclose($file);

// Read data from text file
$file = fopen('data.txt', 'r');
$data = fgets($file);
fclose($file);

echo $data;
?>