How can optional parameters be handled in PHP functions?
Optional parameters in PHP functions can be handled by assigning default values to those parameters. This allows the function to be called without providing a value for the optional parameter, in which case the default value will be used. This provides flexibility and allows for more customizable function calls.
function greet($name, $greeting = "Hello") {
echo $greeting . ", " . $name . "!";
}
greet("Alice"); // Output: Hello, Alice!
greet("Bob", "Good morning"); // Output: Good morning, Bob!
Related Questions
- What best practices should be followed when dynamically replacing content in emails using PHP?
- What is the significance of using GROUP BY in SQL queries, and how does it affect the results when combined with SELECT?
- How can the issue of only retrieving one entry despite using LIMIT in PHP be resolved when querying a database table?