How to find the difference between the maximum value and the minimum value of an array in php

青灯夜游
Release: 2023-03-16 14:26:01
Original
1799 people have browsed it

Steps to find the difference: 1. Use the max() function to get the maximum value of the array, the syntax "max($arr)"; 2. Use the min() function to get the minimum value of the array, the syntax "min($ arr)"; 3. Use the "-" operator to subtract the obtained maximum value and minimum value to calculate the difference. The syntax is "maximum value - minimum value".

How to find the difference between the maximum value and the minimum value of an array in php

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

php finds the largest array Method of difference between value and minimum value

In PHP, you can use the max() and min() functions to find the maximum and minimum values ​​of the array, and then combine the maximum and minimum values Subtract to find the difference.

Implementation steps:

Step 1: Use the max() function to obtain the maximum value of the array

max() function can Return the maximum value in an array

<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(2,3,4,5,67,8,32,1,23,45,0); 
var_dump($arr);
$max=max($arr);
echo "数组最大值为: ".$max;
?>
Copy after login

How to find the difference between the maximum value and the minimum value of an array in php

Step 2: Use the min() function to get the minimum value of the array

min() The function returns the minimum value in an array

$min=min($arr);
echo "数组最小值为: ".$min;
Copy after login

How to find the difference between the maximum value and the minimum value of an array in php

Step 3: Use the "-" operator to subtract the obtained maximum value and minimum value to calculate the difference

最大值 - 最小值
Copy after login

Complete implementation example code:

<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(2,3,4,5,67,8,32,1,23,45,0); 
var_dump($arr);
$max=max($arr);
echo "数组最大值为: ".$max."<br>";
$min=min($arr);
echo "数组最小值为: ".$min."<br>";
$res=$max-$min;
echo "数组最大值和最小值的差值为:".$res;
?>
Copy after login

How to find the difference between the maximum value and the minimum value of an array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to find the difference between the maximum value and the minimum value of an array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!