PHP Function Parameters
Summary: in this tutorial, you’ll learn about the function parameters and pass arguments by value and reference.
Introduction to the PHP function parameters
A function can have zero or more parameters:
function function_name(parameter_list)
{
}
Code language: HTML, XML (xml)
When a function has multiple parameters, you need to separate them using a comma (,
).
The following example defines the concat()
function that concatenates two strings into one:
function concat($str1, $str2)
{
return $str1 . $str2;
}
Code language: HTML, XML (xml)
The concat()
function has two parameters $str1
and $str2
.
When you call the concat()
function, you need to pass two arguments that correspond to the parameters. For example:
function concat($str1, $str2)
{
return $str1 . $str2;
}$greeting = concat('Welcome ', 'Admin');
echo $greeting;
Code language: HTML, XML (xml)
In this example, the $str1
will take the first argument 'Welcome '
, and the $str2
will take the second argument 'Admin'
.
PHP will raise an error if the number of arguments you pass to the function is less than the number of parameters. For example:
function concat($str1, $str2)
{
return $str1 . $str2;
}
$greeting = concat('Welcome');
echo $greeting;
Code language: HTML, XML (xml)
When you pass multiple arguments to a function, you can break the list the arguments vertically to make the code more readable like this:
function concat($str1, $str2)
{
return $str1 . $str2;
}
$greeting = concat(
'Welcome ',
'Home'
);
echo $greeting;
Code language: HTML, XML (xml)
It’s a good practice to list arguments vertically when the argument list is long.
Trailing comma (,)
From PHP 7.0, the argument list may contain a trailing comma (,
) which the PHP interpreter will ignore. For example:
$greeting = concat(
'Welcome ',
'Home',
);
Code language: PHP (php)
Starting from PHP 8.0, you can place the trailing comma (,) in the parameter list like this:
function concat(
$str1,
$str2,
) {
return $str1 . $str2;
}
Code language: PHP (php)
Passing arguments by values
Consider the following example:
$counter = 1;
function increase($value)
{
$value+= 1;
echo $value. <br>; // 2
}
// increase the counter
increase($counter);
echo $counter . <br>; // 1
Code language: HTML, XML (xml)
Output:
2
1
How it works.
- First, define the
$counter
variable and initialize its value to one. - Second, define the
increase()
function that increases the argument by one and displays it. - Third, call the
increase()
function and pass the$counter
variable into the function. - Finally, display the
$counter
variable.
When you pass the $counter
variable to the increase()
function, the function increases its value by one. Therefore, when you display the value of the $counter
inside the function, you’ll get two.
However, after the function call, the value of the counter is still one. It means that the increase()
function doesn’t increase the $counter
variable outside the function.
What happens is that when you pass the $counter
to the increase()
function, the function copies the $counter
variable and modifies the copy. It doesn’t change the original variable. The $counter
variable doesn’t change.
When the value of an argument within the function is changed and doesn’t get changed outside the function, it is passed by value.
By default, arguments are passed by values in PHP. If you want a function to change its arguments, you need to pass the arguments by reference.
Passing arguments by reference
To pass an argument by reference, you prepend the operator (&
) to the parameter name in the function definition like this:
$counter = 1;
function increase( &$value )
{
$value += 1;
echo $value . <br>; // 2
}
// increase the counter
increase($counter);
echo $counter . <br>; // 2
Code language: HTML, XML (xml)
Output:
2
2
In this example, the change of the $counter
variable reflects both inside and outside the function.
Summary
- Separate parameters by a comma (
,
). Since PHP 8.0, the parameter list can have the trailing comma (,
) which the PHP interpreter ignores. - By default, arguments are passed by value in PHP.
- Prepend parameters by an ampersand (
&
) to pass arguments by reference.