PHP Program to Find Largest Number in Array
In PHP, you can find the largest number in an array using the built-in function
max($array), for example: $largest = max($array);.Examples
Input[1, 2, 3, 4, 5]
Output5
Input[-10, 0, 5, 3, 9]
Output9
Input[7]
Output7
How to Think About It
To find the largest number in an array, look at each number one by one and remember the biggest number you have seen so far. Compare each new number with the current biggest and update it if the new number is bigger.
Algorithm
1
Start with the first number as the largest.2
Go through each number in the array one by one.3
If a number is bigger than the current largest, update the largest to this number.4
After checking all numbers, return the largest number found.Code
php
<?php $array = [3, 5, 1, 9, 2]; $largest = max($array); echo "Largest number is: " . $largest; ?>
Output
Largest number is: 9
Dry Run
Let's trace the array [3, 5, 1, 9, 2] through the code using the max function.
1
Initial array
Array is [3, 5, 1, 9, 2]
2
Find max
max([3, 5, 1, 9, 2]) returns 9
3
Print result
Output is 'Largest number is: 9'
| Iteration | Current Number | Largest So Far |
|---|---|---|
| 1 | 3 | 3 |
| 2 | 5 | 5 |
| 3 | 1 | 5 |
| 4 | 9 | 9 |
| 5 | 2 | 9 |
Why This Works
Step 1: Use max function
The max() function in PHP quickly finds the largest value in an array without manual looping.
Step 2: Store result
We save the largest number found into a variable to use or display later.
Step 3: Print output
Using echo, we show the largest number to the user.
Alternative Approaches
Manual loop to find largest
php
<?php $array = [3, 5, 1, 9, 2]; $largest = $array[0]; foreach ($array as $num) { if ($num > $largest) { $largest = $num; } } echo "Largest number is: " . $largest; ?>
This method shows how to find the largest number without built-in functions, useful for learning or custom logic.
Sort array and pick last element
php
<?php $array = [3, 5, 1, 9, 2]; sort($array); echo "Largest number is: " . $array[count($array) - 1]; ?>
Sorting the array and taking the last element also finds the largest number but is less efficient than max() for this task.
Complexity: O(n) time, O(1) space
Time Complexity
The program checks each element once, so the time grows linearly with the number of elements, making it O(n).
Space Complexity
It uses only a few extra variables regardless of input size, so space complexity is O(1).
Which Approach is Fastest?
Using max() is fastest and simplest; manual looping is similar but more code; sorting is slower due to extra operations.
| Approach | Time | Space | Best For |
|---|---|---|---|
| max() function | O(n) | O(1) | Simple and fast for any array size |
| Manual loop | O(n) | O(1) | Learning how to find max without built-ins |
| Sorting array | O(n log n) | O(1) | When array needs sorting anyway |
Use PHP's built-in
max() function for the simplest and fastest way to find the largest number in an array.Beginners often forget to handle empty arrays, which can cause errors when using
max().