How can optional parameters be set in PHP functions similar to VB?
In PHP, optional parameters can be set by assigning a default value to the function parameter. This default value will be used if no argument is passed for that parameter when the function is called. This is similar to how optional parameters are set in VB.
function greet($name, $greeting = "Hello") {
echo $greeting . ", " . $name . "!";
}
greet("John"); // Output: Hello, John!
greet("Jane", "Hi"); // Output: Hi, Jane!
Keywords
Related Questions
- What are some best practices for avoiding duplicate output when working with arrays in PHP?
- In PHP, what are some alternative methods to dynamically retrieve data from classes, as suggested in the forum thread?
- Are there any alternative approaches to handling sessions in PHP other than the default session handler?