SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
PHP Functions
Outline
• Functions
• Function declaration
• Function Arguments
• Variable number of arguments
• Returning Values
• Variable Scope
• Static variables
• Recursion
• Tricks
• Useful Built-in Functions
• Assignment
Functions
• Is a sub program.
• A block of code that is defined under a certain name
( function name ).
• Can take arguments.
• Can return a value.
• Function name has the same naming rules as variables
and constants.
• Function names are not case sensitive.
Function declaration
function doSomething($arg_1, $arg_2, /* ..., */ $arg_n)
{
echo “something.n";
return $val;
}
Name Arguments
Body
Return
Function Arguments
• Passing by value (default – except for objects) :
function multiply($x, $y)
{
echo $x * $y;
}
Function Arguments
• Passing by reference :
$x = 10;
$y = 5;
$result = 0;
multiply( $x, $y , $result );
echo $result; // 50
function multiply($x, $y, &$result)
{
$result = $x * $y;
}
Function Arguments
• Default argument values:
function doIt( $name = “PHP” )
{
echo “Hello ” . $name;
}
Can be called :
doIt(); // Hello PHP
doIt( “Guys” ); // Hello Guys
• It is preferable that the arguments with default values
should be the last arguments passed to the function.
Function Arguments
• PHP emits a warning if the number of the arguments
passed is not the same the number of arguments in the
function declaration.
• PHP does not throw any warning in case the number is
bigger than the arguments in the function declaration.
function doIt( $name )
{
echo “Hello ” . $name;
}
doIt(); // a warning is emitted
doIt( “Elephant”, “Cow” ); // this is OK
Demo
Variable number of arguments
• PHP allows you to create a function with unlimited
number of arguments.
function displayAll( )
{
$numargs = func_num_args();
$arg_list = func_get_args();
for ($i = 0; $i < $numargs; $i++) {
echo $arg_list[$i] . “ ”;
}
}
displayAll( “This” , “is”, “a”, “good”, “thing.” );
Returning Values
• Functions may return values :
function doIt( $name )
{
return “Hello ” . $name;
}
echo doIt(“PHP”); // Hello PHP
Variable Scope
$a = 1; /* global scope */
function test()
{
echo $a; /* reference to local scope variable */
}
test(); // echoes an empty string
Variable Scope
The GLOBAL keyword:
$a = 1; /* global scope */
function test()
{
global $a; // can also use the $GLOBALS array
echo $a; /* reference to local scope variable */
}
test(); // 1
Static variables
• Static variables are initialized only one time.
function test()
{
static $a = 0;
echo $a;
$a++;
}
test(); //0
test(); // 1
test(); // 2
Recursion
• Recursion occurs when something contains, or uses, a
similar version of itself.
• In programming, it happens when a function is having
a call to itself.
• The factorial problem :
• 5! = 5 x 4 x 3 x 2 x 1 = 120
Recursion
function factorial($number) {
if ($number == 0) return 1;
return $number * factorial($number - 1);
}
echo factorial(6); // 720
Tricks
A function inside an IF statement :
if ($condition) {
function doSomething()
{
echo “Will only be defined if the IF condition is true";
}
// some code
doSomething();
}
Tricks
A function inside another function:
function doSomething1()
{
function doSomething2()
{
echo "I can only be called from inside doSomething1
n";
}
// some code
doSomething2();
}
Tricks
Variable functions:
function display( $x )
{
echo $x;
}
$name = “display”;
$name( “PHP” ); //PHP
Tricks
Anonymous functions:
$greet = function($name)
{
echo “Hello “ . $name;
};
$greet(“PHP”); // Hello PHP
Useful Built-in Functions
• boolean function_exists( $name )
• Checks whether there is a function with that
$name.
• void get_defined_functions()
• Gets a list of all names of defined functions in the
script.
Assignment
- Write a PHP function that calculates the average of passed
parameters (The number of parameters is dynamic).
What's Next?
• Arrays.
Questions?

More Related Content

PPTX
Php.ppt
PPT
Php forms
PPTX
Form Handling using PHP
PPT
Oops concepts in php
PDF
Php introduction
PPT
PPT
Introduction To PHP
Php.ppt
Php forms
Form Handling using PHP
Oops concepts in php
Php introduction
Introduction To PHP

What's hot (20)

PDF
Chap 4 PHP.pdf
PPT
PHP variables
PPTX
Operators php
PDF
4.2 PHP Function
PPTX
Css position
PPTX
PPTX
Laravel ppt
PPT
What Is Php
 
PPT
Class 5 - PHP Strings
PPSX
Javascript variables and datatypes
PPTX
Php operators
PPT
01 Php Introduction
PPTX
Method overloading
PPTX
ASP.NET MVC Presentation
PPT
Php with MYSQL Database
PPT
Java Script ppt
PPT
PHP - Introduction to PHP Forms
PPT
Oops in PHP
PDF
A Basic Django Introduction
Chap 4 PHP.pdf
PHP variables
Operators php
4.2 PHP Function
Css position
Laravel ppt
What Is Php
 
Class 5 - PHP Strings
Javascript variables and datatypes
Php operators
01 Php Introduction
Method overloading
ASP.NET MVC Presentation
Php with MYSQL Database
Java Script ppt
PHP - Introduction to PHP Forms
Oops in PHP
A Basic Django Introduction
Ad

Viewers also liked (7)

PDF
Sorting arrays in PHP
PDF
Functions in PHP
PDF
Php Tutorials for Beginners
PDF
Abstract Class and Interface in PHP
PPTX
PHP Functions & Arrays
PPT
Functions in php
PDF
Php tutorial
Sorting arrays in PHP
Functions in PHP
Php Tutorials for Beginners
Abstract Class and Interface in PHP
PHP Functions & Arrays
Functions in php
Php tutorial
Ad

Similar to Class 3 - PHP Functions (20)

PPT
PHP-03-Functions.ppt
PPT
PHP-03-Functions.ppt
PPT
Understanding PHP Functions: A Comprehensive Guide to Creating
PPT
PHP - Introduction to PHP Functions
PPTX
function in php like control loop and its uses
PPTX
PHP FUNCTIONS AND ARRAY.pptx
PPT
PHP Fuctions.ppt,IT CONTAINS PHP INCLUDE FUNCTION
PPTX
Functions in PHP.pptx
PPTX
function in php using like three type of function
PPTX
Arrays &amp; functions in php
PDF
Php, mysq lpart3
PDF
PHP Unit 3 functions_in_php_2
PPT
Php Reusing Code And Writing Functions
PDF
Web app development_php_06
PPT
Php Chapter 2 3 Training
PPTX
Web Application Development using PHP Chapter 3
PPTX
Functuon
PPTX
Functuon
PDF
php AND MYSQL _ppt.pdf
PPTX
UNIT- 2 Functions__________________.pptx
PHP-03-Functions.ppt
PHP-03-Functions.ppt
Understanding PHP Functions: A Comprehensive Guide to Creating
PHP - Introduction to PHP Functions
function in php like control loop and its uses
PHP FUNCTIONS AND ARRAY.pptx
PHP Fuctions.ppt,IT CONTAINS PHP INCLUDE FUNCTION
Functions in PHP.pptx
function in php using like three type of function
Arrays &amp; functions in php
Php, mysq lpart3
PHP Unit 3 functions_in_php_2
Php Reusing Code And Writing Functions
Web app development_php_06
Php Chapter 2 3 Training
Web Application Development using PHP Chapter 3
Functuon
Functuon
php AND MYSQL _ppt.pdf
UNIT- 2 Functions__________________.pptx

More from Ahmed Swilam (6)

PPTX
Class 8 - Database Programming
PPT
Class 7 - PHP Object Oriented Programming
PPT
Class 6 - PHP Web Programming
PPT
Class 4 - PHP Arrays
PPT
Class 2 - Introduction to PHP
PPT
Class 1 - World Wide Web Introduction
Class 8 - Database Programming
Class 7 - PHP Object Oriented Programming
Class 6 - PHP Web Programming
Class 4 - PHP Arrays
Class 2 - Introduction to PHP
Class 1 - World Wide Web Introduction

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Cloud computing and distributed systems.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Big Data Technologies - Introduction.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Modernizing your data center with Dell and AMD
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Approach and Philosophy of On baking technology
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation theory and applications.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Cloud computing and distributed systems.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
20250228 LYD VKU AI Blended-Learning.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Spectral efficient network and resource selection model in 5G networks
The Rise and Fall of 3GPP – Time for a Sabbatical?
Big Data Technologies - Introduction.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Modernizing your data center with Dell and AMD
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Approach and Philosophy of On baking technology
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Building Integrated photovoltaic BIPV_UPV.pdf
Machine learning based COVID-19 study performance prediction
Encapsulation theory and applications.pdf
The AUB Centre for AI in Media Proposal.docx
Diabetes mellitus diagnosis method based random forest with bat algorithm

Class 3 - PHP Functions

  • 2. Outline • Functions • Function declaration • Function Arguments • Variable number of arguments • Returning Values • Variable Scope • Static variables • Recursion • Tricks • Useful Built-in Functions • Assignment
  • 3. Functions • Is a sub program. • A block of code that is defined under a certain name ( function name ). • Can take arguments. • Can return a value. • Function name has the same naming rules as variables and constants. • Function names are not case sensitive.
  • 4. Function declaration function doSomething($arg_1, $arg_2, /* ..., */ $arg_n) { echo “something.n"; return $val; } Name Arguments Body Return
  • 5. Function Arguments • Passing by value (default – except for objects) : function multiply($x, $y) { echo $x * $y; }
  • 6. Function Arguments • Passing by reference : $x = 10; $y = 5; $result = 0; multiply( $x, $y , $result ); echo $result; // 50 function multiply($x, $y, &$result) { $result = $x * $y; }
  • 7. Function Arguments • Default argument values: function doIt( $name = “PHP” ) { echo “Hello ” . $name; } Can be called : doIt(); // Hello PHP doIt( “Guys” ); // Hello Guys • It is preferable that the arguments with default values should be the last arguments passed to the function.
  • 8. Function Arguments • PHP emits a warning if the number of the arguments passed is not the same the number of arguments in the function declaration. • PHP does not throw any warning in case the number is bigger than the arguments in the function declaration. function doIt( $name ) { echo “Hello ” . $name; } doIt(); // a warning is emitted doIt( “Elephant”, “Cow” ); // this is OK
  • 10. Variable number of arguments • PHP allows you to create a function with unlimited number of arguments. function displayAll( ) { $numargs = func_num_args(); $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) { echo $arg_list[$i] . “ ”; } } displayAll( “This” , “is”, “a”, “good”, “thing.” );
  • 11. Returning Values • Functions may return values : function doIt( $name ) { return “Hello ” . $name; } echo doIt(“PHP”); // Hello PHP
  • 12. Variable Scope $a = 1; /* global scope */ function test() { echo $a; /* reference to local scope variable */ } test(); // echoes an empty string
  • 13. Variable Scope The GLOBAL keyword: $a = 1; /* global scope */ function test() { global $a; // can also use the $GLOBALS array echo $a; /* reference to local scope variable */ } test(); // 1
  • 14. Static variables • Static variables are initialized only one time. function test() { static $a = 0; echo $a; $a++; } test(); //0 test(); // 1 test(); // 2
  • 15. Recursion • Recursion occurs when something contains, or uses, a similar version of itself. • In programming, it happens when a function is having a call to itself. • The factorial problem : • 5! = 5 x 4 x 3 x 2 x 1 = 120
  • 16. Recursion function factorial($number) { if ($number == 0) return 1; return $number * factorial($number - 1); } echo factorial(6); // 720
  • 17. Tricks A function inside an IF statement : if ($condition) { function doSomething() { echo “Will only be defined if the IF condition is true"; } // some code doSomething(); }
  • 18. Tricks A function inside another function: function doSomething1() { function doSomething2() { echo "I can only be called from inside doSomething1 n"; } // some code doSomething2(); }
  • 19. Tricks Variable functions: function display( $x ) { echo $x; } $name = “display”; $name( “PHP” ); //PHP
  • 20. Tricks Anonymous functions: $greet = function($name) { echo “Hello “ . $name; }; $greet(“PHP”); // Hello PHP
  • 21. Useful Built-in Functions • boolean function_exists( $name ) • Checks whether there is a function with that $name. • void get_defined_functions() • Gets a list of all names of defined functions in the script.
  • 22. Assignment - Write a PHP function that calculates the average of passed parameters (The number of parameters is dynamic).