Friday 11 December 2015

Function in PHP

function in PHP
Function are self contained block of statement which used to perform any specific task.
A function is a block of statements that can be used repeatedly in a program.
A function will not execute immediately when a page loads.
A function will be executed by a call to the function.
Types of Function:-
system defined/library/inbuilt
user defined

Advantages Of Function
function is created once but used many times, often from more than one program.
It reduces duplication within a program.

Debugging and testing a program becomes easier when the program is subdivide.
 
How to define Function
A function will be executed by a call to the function.
Syntax:-
function function_name( )
{
  code to be executed;
}
Note: A function name can start with a letter or underscore (not a number).

Example:-
<?php
//define function and implements it
function f1()
{
  echo “Hello";
}
//call function whenever you need
f1();
?>

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 seperate them with a comma.
<?php
function familyName($fname)
{
    echo "Hello ".$fname."..<br>";
}
familyName("Jani");
familyName("Hege");
?>
PHP Default Argument Value

how to use a default parameter, If we call the function.
<?php
function setHeight($minheight=50) {
  echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>

PHP Functions - Returning values
To let a function return a value, use the return statement. 
<?php
function sum($x,$y)
{
  $z=$x+$y;
  return $z;
}
echo "5 + 10 = " . sum(5,10) . "<br>";
echo "7 + 13 = " . sum(7,13) . "<br>";
echo "2 + 4 = " . sum(2,4);
?>

Scope Of Variables
There are 3 types of variables i.e used inside a function:-
Local
Global
Static

Local Variable
Local variables are always initialize inside the function body. Local variable cant work outside function body.
<?php
function add()
 {
  $x=100;
  $y=200;
  $sum=$x+$y;
  echo "sum of given no=".$sum;
}
add();
?>
Output : Sum of given no = 300
Here define a variable $x and $y . Variable $x and $y has been defined inside add( ) function so these variables are not accessible outside the add( ) function.


Global Variable
Global variables are always initialize outside the function body.
Global variable work inside the function body as well as outside function body.
To access Global variable we have to use global keyword.
<?php
$x=100;
$y=200;
function add()
{
 global $x,$y;
$sum=$x+$y;
echo "sum of given no=".$sum;
}
add();
?>
There are two variables declare $x and $y outside the function body.
Now if we want to call these global variable $x and $y inside add( ) and sub( ) function
then used “global” keyword before the variable name like “global $x” and “global $y”.


Static Variable
When a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted then use static keyword.
<?php
function Test()
{
  static $x=0;
  echo $x;
  $x++;
}
Test();
Test();
Test();
?>
Output : 0 1 2

PHP array
All the variables you have used have held only a single value.
Array variables are “special” because they can hold more than one value in one single variable.
This makes them particularly useful for storing related values.
There are three types of array:-
Indexed array : An array with a numeric index
Associative array : An array where each key is associated with a value
Multidimensional array : An array containing one or more array