# PHP Function arguments

PHPServer Side ProgrammingProgramming

## Introduction

A function in PHP can be defined to accept input from calling environment/script in the form of arguments. These arguments are given as comma separeted list inside the parentheses in front of name of function. Note that while calling a function, same number of arguments must be passed to it.

PHP supports calling a function by passing value, reference, arguments with default value and by passing variable number of arguments.

## function with arguments

In following example, a function is defined with two formal arguments. When this function is called by passing the arguments by value. Arguments of function become its local variables. Hence, any change in value of argument inside a function doesn't reflect outside it.

Here, value of $x is changed inside the function, but if we check its value after call to function, it has not changed ## Example Live Demo <?php function add($x, $y){$x= $x+$y ;
echo $x . "\n"; }$x=10;
$y=20; add($x,$y); //outside function$x has previous value.
echo $x; ?> ## Output This will produce following result. − 30 10 ## Passing array to function In following example, add() function is defined to receive array as argument. Inside the function, array elements are traversed using foreach loop ## Example Live Demo <?php function add($arr){
$sum=0; foreach ($arr as $i){$sum+=$i; } echo "sum = " .$sum;
}
?>

## Output

This will produce following result. −

sum = 6

## Passing arguments by reference

Values are passed to a function's arguments by value. Hence, changes to argument's value inside function are not reflected outside it. When arguments are passed by reference, changes are carried to the argument's value outside it.

$y=7; echo "before calling function x=$x y=$y\n"; swap($x, $y); echo "after calling function x=$x y=$y\n"; ?> ## Output This will produce following result. − before calling function x=5 y=7 inside function x=7 y=5 after calling function x=7 y=5 Values of variables$x and $y are interchanged in swap() function. Since, variables are passed by reference, the variables show modified values outside the function too ## Type hints (Type declarations) PHP in fact is a dynamically typed language. Hence, it is not necessary to declare a variable with its type (as in C/C++ or Java). However, type declaration of arguments in a function llows the parser to detect incorrect data types passed to the function. ## Syntax //define a function with type hints function myfunction(type$arg1, type $arg2){ .. .. } All standard PHP data types including scalar types, array, class/interface, iterable and object are valid types for providing type hints in a function declaration ## Example Live Demo <?php function add(...$numbers){
$ttl=0; foreach ($numbers as $num){$ttl=$ttl+$num;
}
return $ttl; }$total=add(10,15,20);
echo "addition=$z\n"; } add(10,20); add(5.55, 6.66); ?> ## Output This will produce following result. − addition=30 addition=11 Note that in second call to add() function, floats are given as arguments, but still no error/warning is displayed. This is because PHP internally coerces float into integer for performing addition. In order to prevent such automatic type conversion, use declareconstruct with strict_types=1 ## Example Live Demo <?php declare(strict_types=1); function add (int$x, int $y){$z=$x+$y;
}
?>
addition=30
PHP Fatal error: Uncaught TypeError: Argument 1 passed to add() must be of the type integer, float given