


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

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

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
