SlideShare a Scribd company logo
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

What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
Nikita Popov
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
Acácio Oliveira
 
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
Manas40552
 
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
SangeetaBorde3
 
Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in php
Ashish Chamoli
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
Php
PhpPhp
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
SulekhJangra
 
PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.ppt
Jamers2
 
PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.ppt
ShishirKantSingh1
 
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
vekariyakashyap
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
KavithaChekuri3
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
LadallaRajKumar
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
NehaSpillai1
 
Function in c
Function in cFunction in c
Chap 4 PHP.pdf
Chap 4 PHP.pdfChap 4 PHP.pdf
Chap 4 PHP.pdf
HASENSEID
 

Similar to Functions in PHP.pptx (20)

What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 
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
 

Recently uploaded

English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 

Recently uploaded (20)

English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 

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.