What is the purpose of using the Bubblesort algorithm in PHP?

The purpose of using the Bubblesort algorithm in PHP is to sort an array of elements in ascending or descending order. This algorithm works by comparing adjacent elements and swapping them if they are in the wrong order. This process is repeated until the array is fully sorted.

function bubbleSort($arr) {
    $n = count($arr);
    for ($i = 0; $i < $n; $i++) {
        for ($j = 0; $j < $n - $i - 1; $j++) {
            if ($arr[$j] > $arr[$j + 1]) {
                $temp = $arr[$j];
                $arr[$j] = $arr[$j + 1];
                $arr[$j + 1] = $temp;
            }
        }
    }
    return $arr;
}

// Example usage
$arr = [64, 34, 25, 12, 22, 11, 90];
$sortedArr = bubbleSort($arr);
print_r($sortedArr);