How to use max() Function In PHP
The max() function is a built-in PHP function that returns the largest value from an array or a set of values.
Example: This example shows the use of the above-mentioned approach.
<?php
// Declare an array
$arr = [10, 20, 30, 40, 50];
// Find the largest element
// using max() function
$largest = max($arr);
echo "The Largest Element is: " . $largest;
?>
Output
The Largest Element is: 50
Explanation:
- $arr is the array containing the elements.
- max($arr) returns the largest value in the array.
How to Find the Largest Element in an Array in PHP?
Given an array containing some elements, the task is to find the largest element from an array in PHP. PHP provides several ways to achieve this, each with its own advantages and use cases.
Below are the approaches to find the largest element in an array in PHP:
Table of Content
- Using max() Function
- Using a Loop
- Using sort() Function
- Using reduce() with Anonymous Function