Presentation On PHP Functions
Download
PHP Functions Presentation Transcript:
1.PHP Functions
2.PHP functions are similar to other programming languages. A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.
You already have seen many functions like fopen() and fread() etc. They are built-in functions but PHP gives you option to create your own functions as well.
Creating a PHP Function
Calling a PHP Function
In fact you hardly need to create your own PHP function because there are already more than 1000 of built-in library functions created for different area and you just need to call them according to your requirement.
3.Creating PHP Function:
Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it.
Following example creates a function called writeMessage() and then calls it just after creating it.
Note that while creating a function its name should start with keyword function and all the PHP code should be put inside { and } braces as shown in the following example below:
4.
Writing PHP Function
function writeMessage()
{
echo "You are really a nice person, Have a nice time!";
}
/* Calling a PHP Function */
writeMessage();
?>
5.This will display following result:
You are really a nice person, Have a nice time!
PHP Functions with Parameters:
PHP gives you option to pass your parameters inside a function.
You can pass as many as parameters your like.
These parameters work like variables inside your function.
Following example takes two integer parameters and add them together and then print them.
6.
Writing PHP Function withParameters
function addFunction($num1, $num2)
{
$sum = $num1 + $num2;
echo "Sum of the two numbers is : $sum";
}
addFunction(10, 20);
?>
7.This will display following result:
Sum of the two numbers is : 30
Passing Arguments by Reference:
It is possible to pass arguments to functions by reference. This means that a reference to the variable is manipulated by the function rather than a copy of the variable's value.
Any changes made to an argument in these cases 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.
Following example depicts both the cases.
8.
Passing Argument by Reference
function addFive($num)
{
$num += 5;
}
function addSix(&$num)
{
$num += 6;
}
$orignum = 10; addFive( &$orignum );
echo "Original Value is $orignum
";
addSix( $orignum );
echo "Original Value is $orignum
";
?>
9.Original Value is 15
Original Value is 21
PHP Functions returning value:
A function can return a value using the return
statement in conjunction with a value or object return
stops the execution of the function and
sends the value back to the calling code.
You can return more than one value from a function
using return array(1,2,3,4).
Following example takes two integer parameters and
add them together and then returns their sum to the
calling program. Note that return keyword is used to
return a value from a function.
10.
Writing PHP Function which returns value
function addFunction($num1, $num2)
{
$sum = $num1 + $num2; return $sum;
}
$return_value = addFunction(10, 20);
echo "Returned value from the function : $return_value
?>
Download
PHP Functions Presentation Transcript:
1.PHP Functions
2.PHP functions are similar to other programming languages. A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.
You already have seen many functions like fopen() and fread() etc. They are built-in functions but PHP gives you option to create your own functions as well.
Creating a PHP Function
Calling a PHP Function
In fact you hardly need to create your own PHP function because there are already more than 1000 of built-in library functions created for different area and you just need to call them according to your requirement.
3.Creating PHP Function:
Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it.
Following example creates a function called writeMessage() and then calls it just after creating it.
Note that while creating a function its name should start with keyword function and all the PHP code should be put inside { and } braces as shown in the following example below:
4.
function writeMessage()
{
echo "You are really a nice person, Have a nice time!";
}
/* Calling a PHP Function */
writeMessage();
?>
5.This will display following result:
You are really a nice person, Have a nice time!
PHP Functions with Parameters:
PHP gives you option to pass your parameters inside a function.
You can pass as many as parameters your like.
These parameters work like variables inside your function.
Following example takes two integer parameters and add them together and then print them.
6.
function addFunction($num1, $num2)
{
$sum = $num1 + $num2;
echo "Sum of the two numbers is : $sum";
}
addFunction(10, 20);
?>
7.This will display following result:
Sum of the two numbers is : 30
Passing Arguments by Reference:
It is possible to pass arguments to functions by reference. This means that a reference to the variable is manipulated by the function rather than a copy of the variable's value.
Any changes made to an argument in these cases 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.
Following example depicts both the cases.
8.
function addFive($num)
{
$num += 5;
}
function addSix(&$num)
{
$num += 6;
}
$orignum = 10; addFive( &$orignum );
echo "Original Value is $orignum
";
addSix( $orignum );
echo "Original Value is $orignum
";
?>
9.Original Value is 15
Original Value is 21
PHP Functions returning value:
A function can return a value using the return
statement in conjunction with a value or object return
stops the execution of the function and
sends the value back to the calling code.
You can return more than one value from a function
using return array(1,2,3,4).
Following example takes two integer parameters and
add them together and then returns their sum to the
calling program. Note that return keyword is used to
return a value from a function.
10.
function addFunction($num1, $num2)
{
$sum = $num1 + $num2; return $sum;
}
$return_value = addFunction(10, 20);
echo "Returned value from the function : $return_value
?>
No comments:
Post a Comment