How can optional parameters be declared in PHP functions or methods?
Optional parameters in PHP functions or methods can be declared by assigning a default value to the parameter in the function definition. This default value will be used if the parameter is not provided when calling the function. This allows for flexibility in function calls, as parameters can be omitted if they are not needed.
function greet($name, $greeting = "Hello") {
echo $greeting . ", " . $name;
}
greet("John"); // Output: Hello, John
greet("Jane", "Hi"); // Output: Hi, Jane