PHP Program to Find Largest and Smallest Element in Array using For loop
ADVERTISEMENTS
PHP program to find largest and smallest element in array using for loop. In this article, you will learn how to make php program to find largest and smallest element in array using for loop.
Source Code
<?php
// PHP Program to Find Largest and Smallest Element in Array using For loop
$x = [73, 42, 423, 42, 342, 14];
// $x - To store array list of input numbers
// $b - To store big element variable
// $sm - To store small element variable
// This will stored the first element of the array to check the element
$b = $x[0];
for ($i = 1; $i < count($x); $i++) {
// This will check one by one with each value of array
// To compare the value is smallest or largest
if ($b < $x[$i]) {
$b = $x[$i];
}
}
echo "\n-----The largest element is: ", $b, "\n";
// This will stored the first element of the array to check the element
$sm = $x[0];
for ($i = 1; $i < count($x); $i++) {
// This will check one by one with each value of array
// To compare the value is smallest or largest
if ($sm > $x[$i]) {
$sm = $x[$i];
}
}
echo "\n-----The smallest element is: ", $sm, "\n";
?>
Output
-----The largest element is: 423
-----The smallest element is: 14