What are the different ways of passing parameters in functions in php?

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.
; // 2 } // increase the counter increase($counter); echo $counter .
; // 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 .
; // 2 } // increase the counter increase($counter); echo $counter .
; // 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.

Did you find this tutorial useful?

What are the different ways of passing parameters to the functions?

There are two ways to pass parameters in C: Pass by Value, Pass by Reference..
Pass by Value. Pass by Value, means that a copy of the data is made and stored by way of the name of the parameter. ... .
Pass by Reference. A reference parameter "refers" to the original data in the calling function..

What is parameter passing in PHP?

Introduction. In PHP, arguments to a function can be passed by value or passed by reference. By default, values of actual arguments are passed by value to formal arguments which become local variables inside the function. Hence, modification to these variables doesn't change value of actual argument variable.

What are function parameters in PHP?

PHP Parameterized functions are the functions with parameters. You can pass any number of parameters inside a function. These passed parameters act as variables inside your function. They are specified inside the parentheses, after the function name.

What are the four types of parameters?

Supported parameter types are string, integer, Boolean, and array.