search
HomeDaily ProgrammingPHP KnowledgePHP calculation displays average temperature, five minimum and maximum temperatures

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].", ";
}

Output:

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

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
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools