PHP function - What is function in php?
A function is a piece of code which takes one or more input in the form of parameter and does some processing and returns a value.
A function is a block of statements that can be used repeatedly in a program.
A function will not execute immediately when a page loads.
A function will be executed by a call to the function.
Creating Function
Calling Function
Creating Function :-
We create a function its name should starts with keyword function and all the php code should be put inside { } braces.
File name : index.php
function show() {
echo "ittutorial is the really nice it tutorial website.";
}
show(); // call the function
?>
Example
File name : index.php
Syntax :-
Function functionName(list of parameters)
{
// function body.
//statements.
return return_value;
}
Example :-
php
function write_name( )
{
echo "mahtab";
}
echo "My name is ";
// calling function
write_name( );
?>
Output : my name is mahtab
Function with parameters :-
A parameter is a just like a variable. Parameter are specified after the function name, inside the parentheses.
File name : index.php
Php
function write_name($fname)
{
echo $fname .?alam?;
}
echo "my name is ";
write_name("mahtab");
echo "my father name is ";
write_name("habib");
?>
Output :-
my name is mahtab
My father name is habib.
Parameterised Function.
File name : index.php
You can pass as many as parameters. These parameters work like variables inside your function.
Eg. Php
function add($num1, $num2)
{
$sum = $num1 + $num2;
echo "result = $sum";
}
add(20,30);
?>
Output :-
PHP Default Argument Value
File name : index.php
function setHeight($minheight = 5) {
echo "your height is : $minheight
";
}
setHeight(6);
setHeight();
setHeight(7);
setHeight(4);
?>
Output :-
your height is : 6
your height is : 5
your height is : 7
your height is : 4
Using default arguments
File name : index.php
You can pass default arguments in the function using an equal sign. For ex.
Function display($greeting, $message = "Hi")
{
echo $greeting;
echo $message;
}
Now if you don't pass anything for the second argument to the display function, the default argument is automatically used.
Example :-
Php
function display($param = null)
{
echo $param;
}
display("hi");
display( );
?>
Functions - Returning values
File name : index.php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) . "
";
echo "7 + 13 = " . sum(7, 13) . "
";
echo "2 + 4 = " . sum(2, 4);
?>
Output :-
5 + 10 = 15
7 + 13 = 20
2 + 4 = 6
File name : index.php
Php
function add($x, $y)
{
$total = $x + $y;
//echo "result = $sum";
return $total;
}
echo "20 + 30 = " .add(20,30);
?>
Output :-
Passing arguments by reference :-
A references to the variable is manipulated by the function. Rather than a copy of the variables value.
Any changes made to an argument in these cases will change the value of the original variable.
You can pass an argument by adding an ampersand (&) to the variable name in either the function call or the function definitions.
File name : index.php
function add($num)
{
$num = $num + 5;
}
function addsix(&$num)
{
$num +=6;
}
$orignum = 10;
add($orignum);
echo "original value is $orignum
";
addsix($orignum);
echo "original value is $orignum
";
?>
Output :-
original value is 15
original value is 21
Dynamic Function call :-
It is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself.
File name : index.php
Php
function say( )
{
echo "hello mahtab
"
}
$holder = "say";
$holder( );
?>
Output :-
Passing array to function
You can pass array to function just like string and numbers.
File name : index.php
$scores = array(65,32,78,98,66);
averager($scores); // calling function
function averager($arr) {
$total = 0;
foreach ($arr as $value) {
$total = $total+$value;
}
if(count($arr)>0) {
echo "the average was ", $total / count($arr);
}
else {
echo "no elements";
} }
?>
Previous
Next