


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.
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(',', $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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
