Function Parameters
Function parameters are variables that are specified in the function definition to accept input values when the function is called. They allow functions to be more flexible and reusable by accepting different inputs and producing different outputs based on those inputs. Here are some key points about function parameters in PHP:
Syntax
Parameters are declared within the parentheses of a function definition.
function functionName($param1, $param2, ...) {
// Function body
}
Number of Parameters
Functions can have zero or more parameters.
- Functions with no parameters:
functionName()
- Functions with multiple parameters:
functionName($param1, $param2, ...)
Type Declaration
PHP supports type declaration for function parameters starting from PHP 7.0. This helps enforce the type of data that can be passed to a function.
function functionName(int $param1, string $param2) {
// Function body
}
Default Values
Parameters can have default values, allowing them to be optional.
function functionName($param1 = defaultValue) {
// Function body
}
Passing Parameters
When calling a function, you provide values for its parameters.
functionName($value1, $value2);