PHP calculation displays average temperature, five minimum and maximum temperatures

藏色散人
Release: 2019-01-29 10:30:12
Original
6057 people have browsed it

PHP calculation displays the average temperature, five minimum and maximum temperatures. The recorded temperatures are 78,60,62,68,71,68,73,85,66,64,76,63,75,76,73,68,62,73,72,65,74,62,62,65 ,64,68,73,75,79,73.

PHP calculation displays average temperature, five minimum and maximum temperatures

Let's use specific code examples to introduce to you the PHP method of calculating the average temperature, the five lowest and the highest temperatures in the above-mentioned recorded temperatures.

Code examples are as follows:

<?php
$month_temp = "78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73,
68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73";
$temp_array = explode(&#39;,&#39;, $month_temp);
$tot_temp = 0;
$temp_array_length = count($temp_array);
foreach($temp_array as $temp)
{
    $tot_temp += $temp;
}
$avg_high_temp = $tot_temp/$temp_array_length;
echo "平均温度为: ".$avg_high_temp."
";
sort($temp_array);
echo " 五个最低温度:";
for ($i=0; $i< 5; $i++)
{
    echo $temp_array[$i].", ";
}
echo "五个最高温度:";
for ($i=($temp_array_length-5); $i< ($temp_array_length); $i++)
{
    echo $temp_array[$i].", ";
}
Copy after login

Output:

平均温度为:70.6                               
五个最低温度:60,62,63,63,64,
五个最高温度:76,78,79,81,85,
Copy after login

Related functions:

explode() The function represents using one string to split another string

count() The function represents counting the number of cells in the array or the number of attributes in the object

sort()The function represents sorting the array. When this function ends the array cells will be rearranged from lowest to highest.

Note:

$tot_temp = $temp is equivalent to $tot_temp=$tot_temp $temp

This article is about PHP calculation and display of average temperature , the introduction of five minimum and maximum temperature methods is also one of the common test points for PHP interviews. It is very simple and easy to understand. I hope it will be helpful to friends in need!

The above is the detailed content of PHP calculation displays average temperature, five minimum and maximum temperatures. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!