Maximum element in an array in PHP


Q. Write a PHP program to find the maximum element in an array

<?php
// Define an array
$numbers = array(12, 45, 78, 34, 89, 23, 67);

// Initialize max with the first element
$max = $numbers[0];

// Traverse the array to find the maximum
for ($i = 1; $i < count($numbers); $i++) {
    if ($numbers[$i] > $max) {
        $max = $numbers[$i];
    }
}

// Display the maximum element
echo "The maximum element in the array is: $max";
?> 

Q. Write an algorithm to find the maximum element in an array in PHP

1. Start
2. Define an array numbers[]
3. Initialize max = numbers[0]
4. Loop from i = 1 to length of array - 1:
    a. If numbers[i] > max, then set max = numbers[i]
5. Print max
6. End 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.