list the biggest number in an array in php

In PHP, you can find the biggest number in an array using the max() function. Here’s an example:

main.php
$numbers = array(4, 2, 10, 8, 22); // Sample array

$biggest_number = max($numbers); // Find the biggest number

echo $biggest_number; // Output: 22
149 chars
6 lines

The max() function takes an array as an argument and returns the highest value in that array.

Alternatively, you can also sort the array in descending order using the rsort() function and then grab the first value of the resulting array.

main.php
$numbers = array(4, 2, 10, 8, 22); // Sample array

rsort($numbers); // Sort the array in descending order

$biggest_number = $numbers[0]; // Get the first value of the array

echo $biggest_number; // Output: 22
212 chars
8 lines

In this example, rsort() is used to sort the array in descending order. We then grab the first value of the resulting array using $numbers[0].

related categories

gistlibby LogSnag