- 01Getting Started
- 02Your First Output (echo)
- 03Working with Variables
- 04Conditionals (if statements)
- 05Loops (for statements)
- 06Writing a Function
- 07Working with Arrays
- 08Loops (while statements)
- 09Classes (Object-Oriented Programming)
- 10Error Handling (try...catch)
- 11Handy Array Functions
- 12Recursive Functions
- 13Using the Standard Library
- 14The switch Statement
- 15The Ternary Operator
- 16Inheritance (Extending a Class)
- 17Writing Comments
- 18Logical Operators (AND, OR, NOT)
- 19Constants (define / const)
- 20Splitting and Joining Strings (explode, implode)
- 21Writing Null-Safe Code (the ?? operator)
- 22Searching an Array (in_array)
- 23Transforming an Array (array_map)
- 24Two-Dimensional Arrays (Tabular Data)
- 25Writing a Custom Exception Class
- 26Default Parameter Values
- 27Using an Array as a Set (Duplicate-Free Data)
- 28Checking Correctness (Your First Step into Testing)
- 29Higher-Order Functions (Passing a Function as an Argument)
- 30Stacks and Queues (Basic Data Structures)
- 31Type Conversion (Casting) Basics
- 32Intro to Regular Expressions (Pattern Matching)
- 33The Binary Search Algorithm
- 34Building a Caesar Cipher (a Character-Shifting Cipher)
- 35Understanding How Bubble Sort Works
- 36Building and Displaying Dates (Basic Year/Month/Day Operations)
- 37Writing Multiple Test Cases Together
- 38Speeding Up Calculations with Memoization (Caching)
- 39Normalizing Strings (trim, Unifying Case)
- 40Shallow Copy vs. Deep Copy
- 41Enum (Enumerated Types) Basics
- 42Flattening an Array
- 43Reversing a String and Checking for Palindromes
- 44Pairing Up Two Arrays (the zip operation)
- 45Rounding Numbers (floor, ceil, round)
- 46Multi-Line Strings (Heredoc)
- 47Functions That Return Multiple Values (Arrays and list())
- 48Finding the GCD and LCM (the Euclidean Algorithm)
- 49Formatting Numbers (Padding Digits, Decimal Places)
- 50Cleanup Logic with try/catch/finally
- 51Functions That Work with Any Type
- 52File Reading and Writing Basics
- 53Generating Random Numbers
- 54Bitwise Operators (AND, OR, XOR, shifts)
- 55Reading Command-Line Arguments
- 56Waiting for a Fixed Amount of Time (sleep)
- 57Watch Out for Floating-Point Rounding Errors
- 58Reading from Standard Input
- 59FizzBuzz (the Classic Practice Problem)
- 60Checking Whether a Number Is Prime
- 61Set Operations with Arrays (Union, Intersection, Difference)
- 62Converting Number Bases (Binary, Hex)
- 63Checking Balanced Parentheses (an Application of Stacks)
- 64Checking Whether Two Words Are Anagrams
- 65Checking Whether a Year Is a Leap Year
- 66Converting Temperature (Celsius to Fahrenheit)
- 67Prime Factorization
- 68[Applied] Build a Simple Inventory Management Tool
Higher-Order Functions (Passing a Function as an Argument)
In this lesson you will learn the idea of higher-order functions in PHP so you can write flexible code that takes a function as an argument. This is for anyone searching for "PHP higher-order function usort how to use".
Passing another function as an argument is what makes something a higher-order function. Picture handing over the rules for a job and letting someone else do the work. Giving a comparison rule to usort() is the classic example.
The sample code passes an anonymous function carrying its own comparison rule to usort(), as in usort($nums, function ($a, $b) { return $b - $a; });, sorting in descending order. The array_map you met earlier is in fact another higher-order function.
A common stumbling block for beginners is what the comparison function's return value means. Returning a negative number means "a comes before b" and a positive number means "a comes after b"; without that, the order will not be what you expected.
Once you can define higher-order functions yourself, you can package shared logic as parts and make your code far more reusable. It is one of the key ideas behind modern PHP style.
๐งช This site can't compile or run PHP directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
