Functions in PHP
 A function is a named block of code that performs
a specific task, possibly acting upon a set of
values given to it, or parameters, and possibly
returning a single value.
 Functions in a PHP program can be either built-in
or user-defined.
PHP User-defined Functions
 A function may be defined using syntax such as
the following:
function functionname()
{
//code to be executed
}
<?php
function sayHello(){
echo "Hello PHP Function";
}
sayHello();//calling function
?>
PHP Function Arguments or
Parameterized function
 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.
<?php
function sayHello($name){
echo "Hello $name<br/>";
}
sayHello(“Amit");
sayHello("Vimal");
sayHello("John");
?>
PHP Function: Returning Value
 PHP functions can return only a single value with
the return keyword:
<?php
function cube($n){
return $n*$n*$n;
}
echo "Cube of 3 is: ".cube(3);
?>
Output: Cube of 3 is: 27
Function Parameters
 Functions can expect, by declaring them in the
function definition, an arbitrary number of
arguments.
 There are two different ways of passing
parameters to a function. The first, and more
common, is by value. The other is by reference.
Call By Value
 The call by value method of passing arguments
to a function copies the actual value of an
argument into the formal parameter of the
function. In this case, changes made to the
parameter inside the function have no effect on
the argument.
Call By Value
<?php
$a=10;
$b=20;
function swap($a,$b)
{
echo "before swapping value of a & b are $a,$b"."<br>";
$temp=$a;
$a=$b;
$b=$temp;
echo "after swapping value of a & b are $a,$b"."<br>";
}
swap($a,$b);
echo "after function call value of a & b are $a,$b";
?>
Call by Reference
 In case of PHP call by reference, actual value is
modified if it is modified inside the function. In
such case, you need to use & (ampersand)
symbol with formal arguments. The & represents
reference of the variable.
Call by Reference
<?php
$a=10;
$b=20;
function swap(&$a,&$b)
{
echo "before swapping value of a & b are $a,$b"."<br>";
$temp=$a;
$a=$b;
$b=$temp;
echo "after swapping value of a & b are $a,$b"."<br>";
}
swap($a,$b);
echo "after function call value of a & b are $a,$b";
?>
Difference between call by value and
call by reference
No
.
Call by value Call by reference
1 A copy of value is passed to the
function
An address of value is passed to
the function
2 Changes made inside the function
is not reflected on other functions
Changes made inside the function
is reflected outside the function
also
3 Actual and formal arguments will be
created in different memory
location
Actual and formal arguments will be
created in same memory location
PHP Default Argument Values
Function
 PHP allows you to define C++ style default argument
values. In such case, if you don't pass any value to
the function, it will use default argument value.
<?php
function greeting($first=“Sunil",$last=“Kumar"){
echo "Greeting: $first $last<br/>";
}
greeting();
greeting("Rahul");
greeting("Michael","Clark");
?>
PHP Function: Default Argument
Value
 We can specify a default argument value in function.
While calling PHP function if you don't specify any
argument, it will take the default argument.
<?php
function sayHello($name=“Anil"){
echo "Hello $name";
}
sayHello("Rajesh");
sayHello();//passing no value
sayHello("John");
?>
Output: Hello Rajesh Hello Anil Hello John
<?php
function add($n1=10,$n2=10)
{
$n3=$n1+$n2;
echo "Addition is: $n3<br/>";
}
add();
add(20);
add(40,40);
?>
Recursion Function
<?php
function factorial($n)
{
if($n==0)
return 0;
if($n==1)
return 1;
if($n>0)
return $n*factorial($n-1);
}
echo "factorial of 5=".factorial(5);
?>
Variable Scope
 The variables defined in a function, including its
parameters, are not accessible outside the function,
and, by default, variables defined outside a function
are not accessible inside the function.
<?php
$a = 3;
function fun( )
{
$a += 2;
}
fun( );
echo $a;?>
Global Variables
If you want a variable in the global scope to be accessible
from within a function, you can use the global keyword. Its
syntax is:
global var1, var2, ...
Changing the previous example to include a global
keyword, we get:
<?php
$a = 3;
function fun( ) {
global $a;
$a += 2;
}
fun( );
echo $a;
?>
Static Variables
 Like C, PHP supports declaring function variables
static. A static variable is shared between all calls
to the function and is initialized during a script’s
execution only the first time the function is called.
 To declare a function variable static, use the static
keyword at the variable’s first use. Typically, the
first use of a static variable is to assign an initial
value.
 static $count = 0;
<?php
function counter( ) {
static $count = 0;
return $count++;
}
for ($i = 1; $i <= 5; $i++) {
print counter( );
}
?>
Output: 01234
 When the function is called for the first time, the
static variable $count is assigned a value of 0.
The value is returned and $count is incremented.
When the function ends, $count is not destroyed
like a non-static variable, and its value remains
the same until the next time counter( ) is called.
The for loop displays the numbers from 0 to 4.

Functions in PHP.pptx

  • 1.
  • 2.
     A functionis a named block of code that performs a specific task, possibly acting upon a set of values given to it, or parameters, and possibly returning a single value.  Functions in a PHP program can be either built-in or user-defined.
  • 3.
    PHP User-defined Functions A function may be defined using syntax such as the following: function functionname() { //code to be executed }
  • 4.
    <?php function sayHello(){ echo "HelloPHP Function"; } sayHello();//calling function ?>
  • 5.
    PHP Function Argumentsor Parameterized function  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. <?php function sayHello($name){ echo "Hello $name<br/>"; } sayHello(“Amit"); sayHello("Vimal"); sayHello("John"); ?>
  • 6.
    PHP Function: ReturningValue  PHP functions can return only a single value with the return keyword: <?php function cube($n){ return $n*$n*$n; } echo "Cube of 3 is: ".cube(3); ?> Output: Cube of 3 is: 27
  • 7.
    Function Parameters  Functionscan expect, by declaring them in the function definition, an arbitrary number of arguments.  There are two different ways of passing parameters to a function. The first, and more common, is by value. The other is by reference.
  • 8.
    Call By Value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
  • 9.
    Call By Value <?php $a=10; $b=20; functionswap($a,$b) { echo "before swapping value of a & b are $a,$b"."<br>"; $temp=$a; $a=$b; $b=$temp; echo "after swapping value of a & b are $a,$b"."<br>"; } swap($a,$b); echo "after function call value of a & b are $a,$b"; ?>
  • 10.
    Call by Reference In case of PHP call by reference, actual value is modified if it is modified inside the function. In such case, you need to use & (ampersand) symbol with formal arguments. The & represents reference of the variable.
  • 11.
    Call by Reference <?php $a=10; $b=20; functionswap(&$a,&$b) { echo "before swapping value of a & b are $a,$b"."<br>"; $temp=$a; $a=$b; $b=$temp; echo "after swapping value of a & b are $a,$b"."<br>"; } swap($a,$b); echo "after function call value of a & b are $a,$b"; ?>
  • 12.
    Difference between callby value and call by reference No . Call by value Call by reference 1 A copy of value is passed to the function An address of value is passed to the function 2 Changes made inside the function is not reflected on other functions Changes made inside the function is reflected outside the function also 3 Actual and formal arguments will be created in different memory location Actual and formal arguments will be created in same memory location
  • 13.
    PHP Default ArgumentValues Function  PHP allows you to define C++ style default argument values. In such case, if you don't pass any value to the function, it will use default argument value. <?php function greeting($first=“Sunil",$last=“Kumar"){ echo "Greeting: $first $last<br/>"; } greeting(); greeting("Rahul"); greeting("Michael","Clark"); ?>
  • 14.
    PHP Function: DefaultArgument Value  We can specify a default argument value in function. While calling PHP function if you don't specify any argument, it will take the default argument. <?php function sayHello($name=“Anil"){ echo "Hello $name"; } sayHello("Rajesh"); sayHello();//passing no value sayHello("John"); ?> Output: Hello Rajesh Hello Anil Hello John
  • 15.
    <?php function add($n1=10,$n2=10) { $n3=$n1+$n2; echo "Additionis: $n3<br/>"; } add(); add(20); add(40,40); ?>
  • 16.
    Recursion Function <?php function factorial($n) { if($n==0) return0; if($n==1) return 1; if($n>0) return $n*factorial($n-1); } echo "factorial of 5=".factorial(5); ?>
  • 17.
    Variable Scope  Thevariables defined in a function, including its parameters, are not accessible outside the function, and, by default, variables defined outside a function are not accessible inside the function. <?php $a = 3; function fun( ) { $a += 2; } fun( ); echo $a;?>
  • 18.
    Global Variables If youwant a variable in the global scope to be accessible from within a function, you can use the global keyword. Its syntax is: global var1, var2, ... Changing the previous example to include a global keyword, we get: <?php $a = 3; function fun( ) { global $a; $a += 2; } fun( ); echo $a; ?>
  • 19.
    Static Variables  LikeC, PHP supports declaring function variables static. A static variable is shared between all calls to the function and is initialized during a script’s execution only the first time the function is called.  To declare a function variable static, use the static keyword at the variable’s first use. Typically, the first use of a static variable is to assign an initial value.  static $count = 0;
  • 20.
    <?php function counter( ){ static $count = 0; return $count++; } for ($i = 1; $i <= 5; $i++) { print counter( ); } ?> Output: 01234
  • 21.
     When thefunction is called for the first time, the static variable $count is assigned a value of 0. The value is returned and $count is incremented. When the function ends, $count is not destroyed like a non-static variable, and its value remains the same until the next time counter( ) is called. The for loop displays the numbers from 0 to 4.