Function PHP
Function PHP
cam
%2Fmovies%2Fwatch-manjummel-boys-malayalam-hd
%2F&e=AT0kl0LDBOlq-NnqZq3N6dw-
a35AdYFgQCu1SQR2v46wBJSRsgOmYzWUh8SST0gY8TresKTHrEs1h1ql
ghnTdAhVyJSZizR7-
YkpbPhWKB5fSgS4tZhnX0dMb1pehhrx7o9wKSTQFRJU4Tx2Dms4QQ
Creating a Function
Syntax:
function function_name(){
executable code;
}
Example:
PHP
<?php
?>
Output:
This is Geeks for Geeks
Example:
PHP
<?php
{
$product = $num1 * $num2 * $num3;
proGeek(2, 3, 5);
?>
Output:
The product is 30
PHP
<?php
// function with default parameter
defGeek("Ram", 15);
// will be considered
defGeek("Adam");
?>
Output:
In the above example, the parameter $num has a default value 12, if
we do not pass any value for this parameter in a function call then this
default value 12 will be considered. Also the parameter $str has no
default value , so it is compulsory.
Functions can also return values to the part of program from where it is
called. The return keyword is used to return value back to the part of
program, from where it was called. The returning value may be of any
type including the arrays and objects. The return statement also marks
the end of the function and stops the execution after that and returns
the value.
Example:
PHP
<?php
?>
Output:
The product is 30
Example:
PHP
<?php
// pass by value
function valGeek($num) {
$num += 2;
return $num;
// pass by reference
function refGeek(&$num) {
$num += 10;
return $num;
$n = 10;
valGeek($n);
refGeek($n);
echo "The original value changes to $n";
?>
Output:
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.
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 . "
";
}
$x=10;
$y=20;
add($x,$y);
//outside function $x has previous value.
echo $x;
?>
Output
This will produce following result. −
30
10
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
Example
Live Demo
<?php
function swap(&$x, &$y){
$t=$x;
$x=$y;
$y=$t;
echo "inside function x=$x y=$y
";
}
$x=5;
$y=7;
echo "before calling function x=$x y=$y
";
swap($x, $y);
echo "after calling function x=$x y=$y
";
?>
Output
This will produce following result. −
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
";
echo "total=". add(1,2,3,4,5). "
";
?>
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
";
}
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
";
}
add(10,20);
add(5.55, 6.66);
?>
Output
addition=30
PHP Fatal error: Uncaught TypeError: Argument 1 passed to add() must be