174. PHP Program to find the Second Biggest and Second Smallest of the given numbers.
How does this program work?
- In this program we are going to learn about how to find the second biggest and second smallest of the given numbers using PHP.
- Each element is compared with the next element, If the element is less than the other in comparison then the elements can be swapped.
- After doing this for each element of the given array, then we get the Output.
Here is the code
<html>
<head>
<title>PHP Program To find the second biggest and second smallest of the given numbers</title>
</head>
<body>
<?php
//Initialize array elements
$a = array(56,89,62,84,14,23 );
$n = count($a);
error_reporting(0);
for ($i=0;$i<=$n;$i++)
{
for ($j=0;$j<=$n;$j++)
{
if ( $a[$i] <= $a[$j] )
{
$temp = $a[$i] ;
$a[$i] = $a[$j] ;
$a[$j] = $temp ;
}
}
}
echo("The Array Elements are: ") ."</br>";
foreach( $a as $b )
{
echo $b ." ";
}
echo("<br> Second Smallest Number is : ".$a[2]) ."<br>";
echo("Second Largest Number is : ".$a[$n-1]) ;
return 0;
?>
</body>
</html>