PHP Functions
To keep the browser from executing a script when the page loads, you can put your script into a function.A function will be executed by a call to the function. You may call a function from anywhere within a page.
Create a PHP Function
A function will be executed by a call to the function.
Syntax
function functionName()
{
code to be executed;
}
PHP function guidelines:
- Give the function a name that reflects what the function does
- The function name can start with a letter or underscore (not a number)
Example
A simple function that writes my name when it is called:
1 2 3 4 5 6 7 8 9 10 11 |
<html> <body> <?php function writeName() { echo "Arbind Mandal"; } echo "My name is "; writeName(); ?> </body> </html> |
Output: My name is Arbind Mandal.
PHP Functions – Adding parameters
To add more functionality to a function, we can add parameters. A parameter is just like a variable.Parameters are specified after the function name, inside the parentheses.
Example 1
The following example will write different first names, but equal last name:
1 2 3 4 5 6 7 8 9 10 11 12 |
<html> <body> <?php function writeName($fname) { echo $fname . " Mandal.<br />"; } echo "My name is "; writeName("Arbind Kumar"); echo "My sister's name is "; writeName("shreya"); echo "My brother's name is "; writeName("Manish"); ?> </body> </html> |
Output:
My name is Arbind Kumar Mandal
My sister’s name is Shreya Mandal
My brother’s name is Manish Mandal
Example 2
The following function has two parameters:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<html> <body> <?php function writeName($fname,$punctuation) { echo $fname . " Mandal" . $punctuation . "<br />"; } echo "My name is "; writeName("Arbind Kumar ","."); echo "My sister's name is "; writeName("Shreya","!"); echo "My brother's name is "; writeName("Manish","?"); ?> </body> </html> |
Output:
My name isArbind Kumar Mandal.
My sister’s name is Shreya MAndal!
My brother’s name is Manish?
Pass by value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php function addfive(&$num) { $num+=5; } function addsix(&$num) { $num+=6; } $orginum=10; addfive($orginum); echo "orginal value is :".$orginum."<br>"; addsix($orginum); echo "orginal value is :".$orginum; ?> |
Pass by reference
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php function addfive(&$num) { $num+=5; } function addsix(&$num) { $num+=6; } $orginum=10; addfive($orginum); echo "orginal value is :".$orginum."<br>"; addsix($orginum); echo "orginal value is :".$orginum; ?> |
WAP to store a information with an array displaying the value of array using function also display the no. of elements using an array
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $age=array("Ram"=>20,"Hari"=>21,"Gita"=>22); {function display($me) asort($me); foreach($me as $key=>$value) { echo " key : ".$key." value : ".$value."<br>"; } $length=count($me); echo "Total no of elements is :".$length; } display($age); ?> |
PHP Functions – Return values
To let a function return a value, use the return statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php function subnum($num,$num2) { $difference=$num-$num2; return $difference; } $result=subnum(20,10); echo "difference of two number :".$result."<br>"; function addnum($num,$num2) { $sum=$num+$num2; return $sum; } $result1=addnum(5,10); echo "sum of the two number :".$result1."<br>"; ?> |