PHP Programs

Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 40: Write a program to sort the elements of an array in descending order.

sort.php

<!DOCTYPE html>  
<html>  
<head>	
	<title>PHP Programs TutorialDuniya.com</title>
</head>
<body>  
	<?php  
		$arr = array(50, 20, 80, 70, 10);   
		$temp = 0;  
		echo "Elements of original array: <br>";  

		for ($i = 0; $i < count($arr); $i++) 
		{   
			echo $arr[$i] . " ";  
		}  
	
		for ($i = 0; $i < count($arr); $i++) 
		{   
			for ($j = $i+1; $j < count($arr); $j++) 
			{   
				if($arr[$i] < $arr[$j]) 
				{  
					$temp = $arr[$i];  
					$arr[$i] = $arr[$j];  
					$arr[$j] = $temp;  
				}   
			}   
		}  
	
		echo "Elements of array sorted in descending order: <br>";  
		for ($i = 0; $i < count($arr); $i++) 
		{   
			echo $arr[$i] . " ";  
		}  
	?>  
</body>  
</html>



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.