What is the syntax for setting default values for function parameters in PHP?
In PHP, you can set default values for function parameters by assigning a default value directly in the function definition. This allows you to call the function without passing in a value for that parameter, in which case the default value will be used. This is useful when you want to provide a default value for an optional parameter.
function greet($name = "Guest") {
echo "Hello, $name!";
}
greet(); // Outputs: Hello, Guest!
greet("John"); // Outputs: Hello, John!
Related Questions
- How does the use of functions like file and fopen contribute to efficiently handling CSV files in PHP?
- What are the potential challenges in maintaining a permanent opt-out for cookies without setting any cookies in PHP?
- What are the best practices for handling client-server interactions in PHP applications to avoid unnecessary data traffic during form input?