This article mainly introduces how to calculate the difference between the maximum value and the minimum value in the PHP array.
Finding the difference between the maximum value and the minimum value in an array is a very simple problem for most PHP people, but for novices, it may not be particularly clear, and this kind of array difference calculation Questions are also very common during PHP interviews.
Let's use a simple code example to explain the method of calculating the difference between the maximum value and the minimum value in a PHP array.
A simple array code example is as follows:
<?php $a = array('123', '334', '123', '99');
First we need to find the maximum and minimum values in the $arr array.
Here we can use the max function and min function.
max: Find the maximum value. If there is only one parameter and it is an array, max() returns the largest value in the array.
min : Find the minimum value. If there is only one parameter and it is an array, min() returns the smallest value in the array.
Then the complete difference code example is as follows:
<?php /** * 计算PHP数组中最大值和最小值的差 */ $a = array('123', '334', '123', '99'); // 获取最大值 echo max($a); echo "<hr>"; // 获取最小值 echo min($a); echo "<hr>"; // 获取差 echo max($a)-min($a);
Access through the browser to find the difference between the maximum value and the minimum value in the PHP arrayThe result is as shown below Display:
This article is a detailed introduction on how to calculate the difference between the maximum value and the minimum value in the PHP array. It is simple and easy to understand. I hope Help those in need!
If you want to know more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!
The above is the detailed content of How to calculate the difference between the maximum value and the minimum value in a PHP array? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!