Function: Function are a set of bloch, which are defined for calling a set of code again and again.
There are two way to calling a function in PHP:
- Directly calling a Function.
- Function called by a object of a class and defined within a class.
They are Important as some of the code, we have to use different location on a site. So a good way is to define in a File i.e. "functions.php" and include it to top of the position.
############################
PHP Code
############################
Program 01: Normal Function just running the code within it
<?php
// Normal Function just running the code within it
// function definition
function callme($a,$b)
{
// echo "<br/>Sum of the ".$a." and ".$b. "is = ".$a+$b;
echo "<br/>Sum of the ".$a." and ".$b. "is = ".($a+$b);
echo "<br/>Multi of the ".$a." and ".$b. "is = ".$a*$b;
echo "<br/>Min of the ".$a." and ".$b. "is = ".$a-$b;
echo "<br/>Divide of the ".$a." and ".$b. "is = ".$a/$b;
echo "<br/>Modulus of the ".$a." and ".$b. "is = ".$a%$b;
}
// function defination
// function call
callme(2,5);
// function call
?>
Program 02: Function definition with returning some value<?php
// function definition with returning some value
function sum($a,$b)
{
$sum = $a + $b;
return $sum;
}
function minus($a,$b)
{
$minus = $a - $b;
return $minus;
}
// function defination with returning some value
// use of return function as a variable
echo "Sum of 2 and 3 is : ".sum(2,3);
// use of return function as a variable
// function definition with returning some value
?>
Program 03: Function definition with returning array value<?php
function callmeforarray($a,$b)
{
$addition
= $a + $b;
$minus
= $a - $b;
$multiplication = $a * $b;
$division
= $a / $b;
$modulus
= $a % $b;
$returnvalue['sum'] = $addition."<br/>";
$returnvalue['min'] = $minus."<br/>";
$returnvalue['mul'] = $multiplication."<br/>";
$returnvalue['div'] = $division."<br/>";
$returnvalue['mod'] = $modulus."<br/>";
return $returnvalue;
}
echo "<hr/>";
$result = callmeforarray(30,5);
echo "Sum is : ". $result['sum'];
echo "Min is : ". $result['min'];
echo "mul is : ". $result['mul'];
echo "div is : ". $result['div'];
echo "mod is : ". $result['mod'];
$result = callmeforarray(34,2);
echo "Sum is : ". $result['sum'];
echo "Min is : ". $result['min'];
echo "mul is : ". $result['mul'];
echo "div is : ". $result['div'];
echo "mod is : ". $result['mod'];
echo "<hr/>";
?>
Program 04: Function definition within in a class<?php
// function definition within in a class
class common
{
function addition($a,$b)
{
echo "<br/>
Sum of the " . $a . " and " . $b . " is : " . ( $a + $b ) ;
}
function multiplication($a,$b)
{
echo "<br/>
Multiplication of the " . $a . " and " . $b . " is : " . ( $a * $b ) ;
}
}
// function defination within in a class
// object creation for class
$com1 = new common();
// object creation for class
// function call via class's object
$com1->addition(2,3);
$com1->multiplication(2,3);
// function call via class's object
?>
Program 05: Function call of a another class<?php
// function call of a another class
class class1
{
function function1()
{
return 123;
}
function function2()
{
return 259;
}
}
class class2
{
function function1()
{
$object = new class1();
echo $object->function1()+$object->function2();
}
}
$obj = new class2();
$obj->function1();
// function call of a another class
echo "<hr/>";
echo "Above Exapmle Basic : " . (123+259);
echo "<hr/>";
?>
############################
Output of the Code
############################
Program 01:
Sum of the 2 and 5is = 7
Multi of the 2 and 5is = 10-5
Divide of the 2 and 5is = 0.4
Modulus of the 2 and 5is = 2
Program 02:
Sum of 2 and 3 is : 5
Program 03:
Sum is : 35
Min is : 25
mul is : 150
div is : 6
mod is : 0
Sum is : 36
Min is : 32
mul is : 68
div is : 17
mod is : 0
Program 04:
Sum of the 2 and 3 is : 5
Multiplication of the 2 and 3 is : 6
Program 05:
382
Above Example Basic : 382