
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP program to find the maximum element in an array
To find the maximum element in an array, the PHP code is as follows −
Example
<?php function get_max_value($my_array){ $n = count($my_array); $max_val = $my_array[0]; for ($i = 1; $i < $n; $i++) if ($max_val < $my_array[$i]) $max_val = $my_array[$i]; return $max_val; } $my_array = array(56, 78, 91, 44, 0, 11); print_r("The highest value of the array is "); echo(get_max_value($my_array)); echo("
"); ?>
Output
The highest value of the array is91
A function named ‘get_max_value()’ is defined, that takes an array as parameter. Inside this function, the count function is used to find the number of elements in the array, and it is assigned to a variable −
$n = count($my_array);
The first element in the array is assigned to a variable, and the array is iterated over, and adjacent values in the array are compared, and the highest value amongst all of them is given as output −
$max_val = $my_array[0]; for ($i = 1; $i < $n; $i++) if ($max_val < $my_array[$i]) $max_val = $my_array[$i]; return $max_val;
Outside the function, the array is defined, and the function is called by passing this array as a parameter. The output is displayed on the screen −
$my_array = array(56, 78, 91, 44, 0, 11); print_r("The highest value of the array is"); echo(get_max_value($my_array));
- Related Articles
- PHP program to find the minimum element in an array
- Program to find the minimum (or maximum) element of an array in C++
- C# program to find maximum and minimum element in an array\n
- C++ Program to Find Maximum Element in an Array using Binary Search
- Program to find maximum XOR with an element from array in Python
- Write a program in Go language to find the element with the maximum value in an array
- Python Program to find the largest element in an array
- Golang Program to Find the Largest Element in an Array
- How to find the maximum element of an Array using STL in C++?
- Recursive program to find an element in an array linearly.
- PHP program to delete an element from the array using the unset function
- Python Program to find largest element in an array
- C# program to find the last matching element in an array
- Program to find largest element in an array in C++
- C# Program to find the smallest element from an array

Advertisements