PHP Online Compiler
Example: PHP Program to Find Largest and Smallest Element in Array using For loop
C
C++
C#
Java
Python
PHP
main.php
STDIN
Run
<?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
Clear
ADVERTISEMENTS