Default Parameter Values
Part of the Fundamentals section of Coddy's PHP journey — lesson 64 of 71.
Sometimes you want a function parameter to have a fallback value when no argument is provided. Default parameter values let you define what a parameter should be if the caller doesn't specify it.
To set a default value, assign it directly in the function declaration using the = operator:
<?php
function greet($name = "Guest") {
echo "Hello, $name!\n";
}
greet("Alice");
greet();
?>This outputs:
Hello, Alice!
Hello, Guest!When you call greet("Alice"), the argument overrides the default. When you call greet() with no argument, PHP uses "Guest" instead.
You can mix required and optional parameters, but parameters with defaults must come after required ones:
<?php
function createUser($name, $role = "member") {
return "$name is a $role";
}
echo createUser("Alice") . "\n";
echo createUser("Bob", "admin");
?>This outputs:
Alice is a member
Bob is a adminThe first parameter $name is required, while $role defaults to "member" if not provided. This pattern makes your functions more flexible without requiring callers to always provide every argument.
Challenge
EasyRead two lines of input:
- A product name (e.g.,
Laptop) - A quantity (e.g.,
5) — this input may be empty
Create a function called formatOrder that accepts two parameters: $product (required) and $quantity (optional, defaults to 1). The function should return a string in the following format:
Order: [quantity]x [product]If the quantity input is empty, call the function with only the product name (using the default quantity). Otherwise, call it with both values.
Print the returned result.
Example 1:
If the inputs are Laptop and 5, the output should be:
Order: 5x LaptopExample 2:
If the inputs are Keyboard and an empty line, the output should be:
Order: 1x KeyboardExample 3:
If the inputs are Monitor and 3, the output should be:
Order: 3x MonitorCheat sheet
Default parameter values allow you to define fallback values for function parameters when no argument is provided.
To set a default value, assign it in the function declaration using =:
<?php
function greet($name = "Guest") {
echo "Hello, $name!\n";
}
greet("Alice"); // Hello, Alice!
greet(); // Hello, Guest!
?>When mixing required and optional parameters, parameters with defaults must come after required ones:
<?php
function createUser($name, $role = "member") {
return "$name is a $role";
}
echo createUser("Alice"); // Alice is a member
echo createUser("Bob", "admin"); // Bob is a admin
?>Try it yourself
<?php
// Read input
$product = trim(fgets(STDIN));
$quantity = trim(fgets(STDIN));
// TODO: Create the formatOrder function with $product (required) and $quantity (optional, defaults to 1)
// The function should return a string in the format: "Order: [quantity]x [product]"
// TODO: Call the function appropriately based on whether quantity is empty or not
// If quantity is empty, call with only product (use default)
// Otherwise, call with both product and quantity
// Print the result
echo $result;
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators