Unit 1 Unit 2 Unit 3 Unit 4 Unit 5 Unit 6 Unit 7 Unit 8 Unit 9
Unit 10
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:
<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:
<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:
<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
<?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
<?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
<?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.
<?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>"; ?>