【杂谈】PHP盘算显现平均温度、五个最低及最高温度
2019-11-18杂谈搜奇网37°c
A+ A-
PHP盘算显现平均温度,五个最低及最高温度。纪录温度是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盘算上述纪录温度中的平均温度、五个最低及最高温度的要领。
代码示例以下:
<?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].", "; }
输出:
平均温度为:70.6 五个最低温度:60,62,63,63,64, 五个最高温度:76,78,79,81,85,
相干函数:
explode() 函数示意运用一个字符串支解另一个字符串
count()函数示意盘算数组中的单位数量,或对象中的属性个数
sort()函数示意对数组举行排序。当本函数结束时数组单位将被从最低到最高重新安排。
注:
$tot_temp += $temp 相当于 $tot_temp=$tot_temp + $temp
本篇文章就是关于PHP盘算显现平均温度、五个最低及最高温度的要领引见,也是PHP口试罕见考点之一,异常简朴易懂,愿望对须要的朋侪有所协助!
以上就是PHP盘算显现平均温度、五个最低及最高温度的细致内容,更多请关注ki4网别的相干文章!