find the range of all elements in an array in php

To find the range of all elements in an array in PHP, you can make use of the min() and max() functions. The range of an array is the difference between the smallest and largest values.

Here's an example code that demonstrates how to find the range of an array:

main.php
<?php
$numbers = array(1, 3, 5, 7, 9); // Sample array

$min = min($numbers); // Find the smallest value in the array
$max = max($numbers); // Find the largest value in the array

$range = $max - $min; // Calculate the range of the array

echo "The range of the array is: " . $range; // Output the range of the array
?>
320 chars
11 lines

This will output:

main.php
The range of the array is: 8
29 chars
2 lines

In this example, we first define an array of numbers. We then use the min() and max() functions to find the smallest and largest values in the array, respectively. Finally, we calculate the difference between the largest and smallest values to obtain the range of the array.

related categories

gistlibby LogSnag