How can PHP be used to calculate the average of values from the previous three days in a text file?

To calculate the average of values from the previous three days in a text file using PHP, we can read the contents of the text file, extract the values for the last three days, calculate their sum, and then divide by 3 to get the average.

<?php
$file = 'data.txt';
$data = file_get_contents($file);
$values = explode("\n", $data);

$last_three_days = array_slice($values, -3);
$sum = array_sum($last_three_days);
$average = $sum / 3;

echo "Average of values from the previous three days: " . $average;
?>