Default Arguments in Functions in PHP
In PHP, default arguments in functions allow you to specify default values for parameters. If a value is not provided for a parameter when calling the function, the default value is used. This provides flexibility when calling functions, as certain parameters can be optional with predefined defaults.
Example:
<?php
// Function with default values for parameters
function greetUser($name = "Guest", $greeting = "Hello") {
echo "$greeting, $name!";
}
// Call the function without providing specific values
greetUser(); // Outputs: Hello, Guest!
// Call the function with specific values
greetUser("John", "Good morning"); // Outputs: Good morning, John!
?>
Output:
Hello, Guest!
Good morning, John!
Explanation
- The function greetUser is defined with two parameters, $name and $greeting, both of which have default values ("Guest" for $name and "Hello" for $greeting).
- Inside the function, it echoes a greeting message using the provided or default values.
- The function greetUser is called without providing specific values for $name and $greeting.
- As a result, the default values ("Guest" and "Hello") are used, and the function echoes: "Hello, Guest!"
- The function greetUser is called with specific values for $name ("John") and $greeting ("Good morning").
- The provided values override the default values, and the function echoes: "Good morning, John!"
Example 2:
<?php
// Function with default values for parameters
function generateGreeting($name, $occasion = "day", $greeting = "Hello") {
echo "$greeting, $name! Have a good $occasion.";
}
// Call the function without providing specific values
generateGreeting("Alice"); // Outputs: Hello, Alice! Have a good day.
// Call the function with specific values
generateGreeting("Bob", "evening", "Good evening"); // Outputs: Good evening, Bob! Have a good evening.
?>
Output:
Hello, Alice! Have a good day.
Good evening, Bob! Have a good evening.
Explanation:
- The generateGreeting function takes three parameters: $name, $occasion, and $greeting. Both $occasion and $greeting have default values.
- If no values are provided for $occasion and $greeting, the default values ("day" for $occasion and "Hello" for $greeting) are used.
- The function is called with different sets of arguments to demonstrate the flexibility of providing specific values or using the default values.
Example 3:
<?php
// Function with default values for parameters
function calculateArea($length, $width = 5) {
$area = $length * $width;
return $area;
}
// Call the function with specific values
$rectangle1 = calculateArea(8, 6);
echo "Area of Rectangle 1: $rectangle1\n"; // Outputs: Area of Rectangle 1: 48
// Call the function without providing width (uses default value)
$rectangle2 = calculateArea(10);
echo "Area of Rectangle 2: $rectangle2\n"; // Outputs: Area of Rectangle 2: 50
?>
Output:
Area of Rectangle 1: 48
Area of Rectangle 2: 50
- The calculateArea function takes two parameters: $length and $width. The $width parameter has a default value of 5.
- If a specific value for $width is provided when calling the function, it overrides the default value. If not provided, the default value is used.
- The function calculates the area of a rectangle using the provided or default values.