Home >Daily Programming >PHP Knowledge >How to calculate the difference between the maximum value and the minimum value in a PHP array? (Pictures + Videos)

How to calculate the difference between the maximum value and the minimum value in a PHP array? (Pictures + Videos)

藏色散人
藏色散人Original
2018-10-11 13:48:118041browse

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(&#39;123&#39;, &#39;334&#39;, &#39;123&#39;, &#39;99&#39;);

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(&#39;123&#39;, &#39;334&#39;, &#39;123&#39;, &#39;99&#39;);

// 获取最大值
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:

How to calculate the difference between the maximum value and the minimum value in a PHP array? (Pictures + Videos)

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!

Statement:
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