Passing argument to function in PHP

Passing argument to function

PHP provide option to pass the values/parameters to the function, you can pass any number of the parameters to the function these parameters are treated as the variables in the function. Scope of the parameters is limited to that function. Function parameters are declared after the function name and inside parentheses. They are declared much like a typical variable would be. Passing argument to function in PHP. In this blog we will discuss the different ways to pass the arguments to the function. If you want to know about the PHP function see our blog on Function in PHP .

Passing Single or Multiple arguments to the function

In PHP you can pass the single or multiple arguments to the function. Argument is like a variable. Information is sent through arguments. You can add as many arguments as you need by separating each with a comma by function. They are specified within the parenthesis inside the function name. You also can pass the object, array as an argument to the function. Passing argument to function in PHP

Function with the single argument

<?php
// In this example we send single argument to function as name
function sayHello($name) {
echo "Hello $name";
}

sayHello("Vishal");
?>

Function with the multiple argument

<?php
function Function($num1, $num2, $num3, $num4) {
$sum = $num1 + $num2;
echo "Addition of $num1 and $num2 is $sum";
$sub = $num3 4 $num4;
echo "Subtraction  of $num3  and $num4  is $sub";
}
Function(10, 20, 40,30);
?>

Passing Arguments by Reference

PHP provide facility to pass arguments to functions by reference. Means we can pass reference to the variable which is manipulated by the function instead of creating  a copy of the variable’s value.

During the function execution any changes made to an argument  will change the value of the original variable. You can pass an argument by reference by adding an ampersand (&) to the variable name in either the function call or the function definition.

<?php
/*Argument passed as value*/
function double_Byvalue($num) {
$num += $num;
}

/*Argument passed by reference*/
function double_Byref(&$num) {
$num += $num;
}

$a = 10;
double_Byvalue( $a );

echo "Value of a after it is passed as value : $a<br />";

double_Byref( $a );
echo "Value of a after it is passed by reference : $a<br />";
?>

This will display following result −

Value of a after it is passed as value : 10
Value of a after it is passed as value : 20

Default Values for Function Parameters

In PHP you can set a parameter to have a default value if the function’s caller doesn’t pass any parameter then this default parameter is used.

<?php
function sayHello($name = 'xyz') {
echo "Hello $name";
}

sayHello("Vishal");
sayHello();
?>

This will display the following result −

Hello Vishal

Hello xyz

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.