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;
}
add(array(1,2,3));
?>

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.

In order to receive value by reference, the argument's name must be prefixed by $ symbol

Example

 Live Demo

<?php
function swap(&$x, &$y){
   $t=$x;
   $x=$y;
   $y=$t;
   echo "inside function x=$x y=$y\n";
}
$x=5;
$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 "total= $total\n";
echo "total=". add(1,2,3,4,5). "\n";
?>

Output

This will produce following result. −

total= 45
total=15

It is also possible to obtain a list of arguments passed to a function with the help of func_get_args() function. We can run a PHP loop to traverse each value in the list of arguments passed. In that case the function definition doesn't have a formal argument.

Example

 Live Demo

<?php
function add (int $x, int $y){
   $z=$x+$y;
   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;
   echo "addition=$z\n";
}
add(10,20);
add(5.55, 6.66);
?>

Second call to add() function will now throw exception −

Output

addition=30
PHP Fatal error: Uncaught TypeError: Argument 1 passed to add() must be of the type integer, float given
raja
Published on 18-Sep-2020 13:38:56
Advertisements