What are the differences between using ASC and DESC in sorting data with PHP?
When sorting data in PHP, using ASC (ascending order) will sort the data in increasing order, while using DESC (descending order) will sort the data in decreasing order. ASC is the default sorting order if not specified.
// Sorting an array in ascending order
$numbers = [5, 2, 8, 1, 3];
sort($numbers);
print_r($numbers); // Output: [1, 2, 3, 5, 8]
// Sorting an array in descending order
$numbers = [5, 2, 8, 1, 3];
rsort($numbers);
print_r($numbers); // Output: [8, 5, 3, 2, 1]
Keywords
Related Questions
- In what situations would using XML as a solution for storing CSS values in a database be more beneficial than using a <textarea> approach in PHP?
- Are there any common pitfalls to avoid when using regular expressions for string manipulation in PHP?
- What are the potential pitfalls of including files in PHP and having to adjust paths?