What is the purpose of using the sort function in PHP and how does it work?

The purpose of using the sort function in PHP is to sort an array in ascending order. It rearranges the elements of the array so that they are in numerical or alphabetical order. The sort function works by comparing values in the array and swapping them if necessary to achieve the desired order.

// Example of using the sort function in PHP
$numbers = array(4, 2, 8, 5, 1);
sort($numbers);

foreach ($numbers as $number) {
    echo $number . " ";
}
// Output: 1 2 4 5 8