<?php
$arr=array(10,3,1,8);
function bubble(&$arr){
$total=count($arr);
for($i=0;$i<$total - 1;$i++){
for($j=0;$total - 1 - $i;$j++){
if($arr[$j] > $arr[$j + 1]){
$temp=$arr[$j];
$arr[$j]=$arr[$j+1];
$arr[$j+1]=$temp;
}
}
echo '<br>';
}
}
bubble($arr);
echo '<br>';
print_r($arr);
?>
The intermediate conditional statement of the second for loop should be $j < $total - 1 - $i
I have not studied the PHP sorting algorithm. You can use the PHP sorting functions sort, asort, rsort, krsort, and ksort to sort arrays, which is simpler.