PHP Notes By Kshitij :: PART 5

Functions:

  • A function is a block of statements that can be used repeatedly in a program.
  • Writing function avoids rewriting the same code over and over.
  • It reduces program size and provides facility to manage and debug the code.
  • A function will not execute immediately when a page loads.
  • A function will be executed by a call of user. Users have to call it by its name and arguments.
  • Function may return a value.
  • Function can return only one value at a time using return keyword.
  • Function uses return keyword to return a value or terminate a function.
  • function keyword is used to define a function.
  • We can pass variable to function by value or by reference.
  • Function name is not case sensitive in PHP.

 

PHP Function Arguments

Information can be passed to functions through arguments. An argument is just like a variable.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

 

There are two types of functions in PHP.

  • User Defined Functions
    1. No accept argument and no return value
    2. Accept argument but no return value
    3. No accept argument but return value
    4. Accept argument and return value

 

  • Library Functions
    1. String Functions
    2. Date & Time Functions
    3. Array Functions

 

 

 

 

 

 

 

 

 

1) User define functions

  1. a) No accept argument and no return value

 

Syntax:

function FunctionName(){

[Statement;]

}

 

Calling function:

              functionName();

 

Example:

 

<?php

function Show(){

echo “Hello iBirds”;

}

 

Show();   //Calling function

?>

 

Output:           Hello iBirds

 

  1. b) Accept argument but no return value

     Syntax for passing argument in function:

 

function FunctionName($arg1, $arg2……$argN){

[Statement;]

}

      Calling function:  functionName($arg1, $arg2……4argN);

                                                                     

Example:

<?php

function ShowMsg($name){

echo “Hello $name”;

}

 

ShowMsg(“India”);

?>

Output:      Hello India

  1. c) No accept argument but return value

    Syntax

function FunctionName(){

[Statement;]

return Value;

}

 

Example

<?php

function basicSalary(){

return 25000;

}

$bs = basicSalary();

echo “Basic Salary is = “.$bs;

?>

Output                 Basic Salary is = 25000

 

 

  1. d) Accept argument and return value

     Syntax

function FunctionName($arg1, $arg2……$argN){

[Statement;]

return value;

}

 

Example

<?php

function getArea($radius){

$area = $radius * $radius * 3.14;

return $area;

}

 

$area=getArea(10);

echo “Total area is : $area”;

?>

 

Output:                Total area is : 314

 

 

 

Default Parameter Function:

Syntax

function function_name($arg1=value1, $arg2=value2……..$argN=valueN){

[Statement;]

[return value;]

}

 

Example

<?php

function Sum($a=10, $b=20, $c=30){

return($a+$b+$c);

}

 

echo Sum();        //Output   60

echo Sum(1);      //Output   51

echo Sum(1, 2);  //Output   33

echo Sum(1, 2, 3);     //Output   6

?>

 

Passing array as argument in function

syntax

function functionName($arrayName){

[statement;]

}

 

Example

<?

function printArray($arr){

foreach($arr as $value){

echo $value.”<br/>”;

}

}

$students = array(“Sunil”, “Himanshu”, “Deepak”, “Priyanka”, “Aditi”, “Dilip”);

$marks = array(58, 92, 76, 99, 69, 44, 55, 66);

$contact = array(9414, 9829, 9424);

printArray($students);     echo “<hr/>”;

printArray($marks);        echo “<hr/>”;

printArray($contact);

?>

 

Passing array as argument and returning array from function

<?php

function getEven($arr){

foreach($arr as $value){

if($value%2==0) $result[]=$value;

}

return $result;

}

$a = array(5, 6, 3, 7, 8, 12, 15, 18);

$data = getEven($a);

foreach($data as $value){

echo “$value<br/>”;

}

?>

 

1) WAP which accept a number and print table of that number

2) WAP which accept a number and print factorial of that number

 

<?php

//Pass a variable to a function by value.

function value($num){

$num++;

echo $num.”<br/>”;

}

$a = 10;

value($a).”<br/>”;

echo $a;

“<br/><br/>”;

 

//You can pass a variable to a function by reference.

function changeValue(&$var)

{

$var++;

}

 

$result=5;

echo $result.”<br/>”;

changeValue($result);

echo $result; // $result is 6 here

?>

We can return more than one value from function.

<?php

//Returning more than one value from function

function netSalary($bs, &$ta, &$da, &$hra, &$ns){

$ta=($bs*10/100);

$da=($bs*50/100);

$hra=($bs*5/100);

$ns=$bs+$ta+$da+$hra;

}

 

$basicSalary=5000; $ta=0; $da=0; $hra=0; $netSalary=0;

netSalary($basicSalary, $ta, $da, $hra, $netSalary);

echo “Basic Salary is : $basicSalary<br/>”;

echo “TA  is : $ta<br/>”;

echo “DA  is : $da<br/>”;

echo “HRA is : $hra<br/>”;

echo “Net Salary is : $netSalary”;

?>

 

Recursive functions

Sometimes the easiest way to model a problem is to make a function call itself – a technique known as recursive function calling.

<?php

function rec($num){

if($num==21) return;

echo $num++.”<br/>”;

rec($num);

}

 

rec(1);

?>

 

Find factorial using function recursion

<?php

function factorial($number) {

if ($number < 2)        return 1;

else                            return ($number * factorial($number-1));

}

echo factorial(5);

?>

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *