How to get the largest element in an array of numbers

<?php
function findLargestElement($numbers) {
    if (empty($numbers)) {
        return null;
    }

    return max($numbers);
}

$numberArray = [5, 12, 8, 3, 10];
$largestElement = findLargestElement($numberArray);

echo "The largest element in the array is: $largestElement";
?>
  • The script defines a function findLargestElement that takes an array of numbers as input and returns the largest element using the max function.
  • If the array is empty, the function returns null.
  • The script then calls the function with a sample array ($numberArray) and prints the result.

Post your Answer