Home Daily Programming PHP Knowledge PHP calculation displays average temperature, five minimum and maximum temperatures

PHP calculation displays average temperature, five minimum and maximum temperatures

Jan 25, 2019 pm 03:02 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)