<?php
/*
* point one: remember swap variable
* point two: remember inside loop size
*/
$arr = array(2,1,4,2,7);//init a array variable
$result = maoSort($arr);//invoke a function and get result
echo '<pre>';//output format
print_r($result);//output result
echo '</pre>';
function maoSort($arr){//function start
$size = count($arr)-1;//need to loop size is total length - 1
for ($i = 0; $i < $size; $i++) {//outside loop
for ($j = 0; $j < $size-$i; $j++) { //inside loop
if ($arr[$j] < $arr[$j+1]) {//compare big or small
$temp = $arr[$j];//start swap
$arr[$j] = $arr[$j+1];
$arr[$j+1] = $temp;
}
}
}
return $arr;
}
?>