SlideShare a Scribd company logo
1 of 21
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.

More Related Content

Similar to Functions in PHP.pptx

Similar to Functions in PHP.pptx (20)

Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in php
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Php
PhpPhp
Php
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
 
PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.ppt
 
PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.ppt
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Function in c
Function in cFunction in c
Function in c
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
Function in c
Function in cFunction in c
Function in c
 
Php, mysq lpart3
Php, mysq lpart3Php, mysq lpart3
Php, mysq lpart3
 
Chap 4 PHP.pdf
Chap 4 PHP.pdfChap 4 PHP.pdf
Chap 4 PHP.pdf
 
Giới thiệu PHP 7
Giới thiệu PHP 7Giới thiệu PHP 7
Giới thiệu PHP 7
 

Recently uploaded

Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 

Recently uploaded (20)

Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 

Functions in PHP.pptx

  • 2.  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.
  • 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 "Hello PHP Function"; } sayHello();//calling function ?>
  • 5. 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"); ?>
  • 6. 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
  • 7. 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.
  • 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; 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"; ?>
  • 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; 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"; ?>
  • 12. 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
  • 13. 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"); ?>
  • 14. 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
  • 15. <?php function add($n1=10,$n2=10) { $n3=$n1+$n2; echo "Addition is: $n3<br/>"; } add(); add(20); add(40,40); ?>
  • 16. 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); ?>
  • 17. 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;?>
  • 18. 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; ?>
  • 19. 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;
  • 20. <?php function counter( ) { static $count = 0; return $count++; } for ($i = 1; $i <= 5; $i++) { print counter( ); } ?> Output: 01234
  • 21.  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.