SlideShare a Scribd company logo
1 of 17
Chapter 2- Functions & Strings
(Part 1)
Functions
1
Functions
2. Function and String
2.1Defining and calling a function
2.2 Default parameters
2.3 Variable parameters, Missing parameters
2.4 Variable function, Anonymous function
2
Functions
Defining a function
function [&] function_name([param[,…]])
{
//code
}
Function name are case insensitive.
If the function is defined with the optional
ampersand then that function returns a reference to
the returned data.
3
Revise –
• Function types-
1. Built-in
2. User defined
• calling to function(return or no return)
• Call by value - f($a);
• Call by reference - &f($a);
Ex.function & f($a)
{
}
$v=& f(10);
4
Functions :Parameter types
1. Default Parameters
2. Variable Parameters
3. Missing Parameters
5
Functions :Parameter types
1. Default Parameters
function pival($pi=3.14)
{
echo “The value of PI is $pi”;
}
The default value is constant.
While calling function,
pival(); // The value of PI is 3.14
pival(null); // The value of PI is
pival(3.00); // The value of PI is 3.00
A function may have any number of parameters
with default values, but they must be listed after all
the parameters.
6
Functions
<?php
function display($types = array("C lang"), $author =
NULL)
{
echo "$types[0]<br/>";
echo $author;
}
display();
display(array("CPP lang", "Java lang"), "ABC");
?>
7
Functions :Parameter types
2. Variable Parameters
When number of parameters are not fixed then
Variable Parameter function can be used. For e.g. to
find out area of different shapes.
PHP provides three functions:
1. func_get_args(): It returns an array of all
parameter provided to the function.
2. func_num_args(): It returns the number of
parameters provided to the function.
3. func_get_arg(): It returns a specific argument
from the parameters.
8
Example-
Variable Parameters
<?php
function area()
{ if (func_num_args() == 0)
{ return false;
}
for ($i = 0; $i < func_num_args(); $i++)
{
$variables += func_get_arg($i);
}
return $variables;
}
echo area(20,20,10,40);
?>
9
Functions :Parameter types
3. Missing Parameters
When you call a function,
you can pass any number of arguments to the
function.
Any parameters the function expects that are not
passed to it remain unset,
and a warning is issued for each of missing parameter.
10
Example-
Missing Parameters
<?php
function display($a, $b)
{ if (isset($a))
{
echo "first argument value is set";
}
if (isset($b))
{
echo "second argument value is set";
}
}display(10,23);
display(20);
display();?>
11
Function Type-1. Variable Function
As with variable variables, you can call a function
based on the value of a variable.
For e.g., a variable is used to determine which of
three functions to call:
switch($which)
{ case 'first': first( ); break;
case 'second': second( ); break;
case 'third': third( ); break; }
In this case, we could use a variable function call to
call the appropriate function. To make a variable
function call, include the parameters for a function in
parentheses after the variable. $f = 'echo'; $f('hello,
world'); // does not work
12
Variable Function
To rewrite the previous example:
$which(); // if $which is "first" the function first( ) is
called, etc... If no function exists for the variable, a
runtime error occurs when the code is evaluated. So,
you can use the built-in function function_exists( ) to
check whether a function exists for the value of the
variable before calling the function:
$yes_or_no = function_exists(function_name); For
example:
if(function_exists($which))
{ $which(); // if $which is "first" the function first( )
is called, etc... }
13
14
<?php
$which='first';
switch($which)
{ //case 'first': first( ); break;
case 'second': second( ); break;
case 'third': third( ); break; }
if(function_exists($which))
{ $which(); }
function first()
{
echo "hiii";
}
?>
Functions Type- 2. Anonymous Function
These functions are localized and temporary & with
no names.
You can create an anonymous function using
create_function( ).
This function takes two parameters-the first shows the
parameters the anonymous function takes in, and the
second is the actual code or functionality.
A randomly generated name for the function is
returned:
$func_name=create_function(args_string,code_string)
15
Example
Anonymous Function
These functions are
<?php
$anonymous_function = create_function('$a, $b',
'return $a*$b;');
print $anonymous_function(3,7);
?>
16
Any Questions?
17

More Related Content

What's hot

OpenWRT Makefile reference
OpenWRT Makefile referenceOpenWRT Makefile reference
OpenWRT Makefile reference家榮 吳
 
02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control EtcGeshan Manandhar
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP FunctionsAhmed Swilam
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06Hassen Poreya
 
Oop php 5
Oop php 5Oop php 5
Oop php 5phpubl
 
Functions in c
Functions in cFunctions in c
Functions in cInnovative
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requireTheCreativedev Blog
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operatorsKhem Puthea
 
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswiftSwift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswiftTomohiro Kumagai
 
Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Bozhidar Boshnakov
 
Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Tomohiro Kumagai
 
C programming function
C  programming functionC  programming function
C programming functionargusacademy
 

What's hot (20)

OpenWRT Makefile reference
OpenWRT Makefile referenceOpenWRT Makefile reference
OpenWRT Makefile reference
 
02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control Etc
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Oop php 5
Oop php 5Oop php 5
Oop php 5
 
Php string function
Php string function Php string function
Php string function
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswiftSwift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
 
Operators php
Operators phpOperators php
Operators php
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
functions of C++
functions of C++functions of C++
functions of C++
 
Function (rule in programming)
Function (rule in programming)Function (rule in programming)
Function (rule in programming)
 
Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)
 
Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)Swift 3.0 の新しい機能(のうちの9つ)
Swift 3.0 の新しい機能(のうちの9つ)
 
C programming function
C  programming functionC  programming function
C programming function
 

Similar to PHP function

Similar to PHP function (20)

php user defined functions
php user defined functionsphp user defined functions
php user defined functions
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Functions
FunctionsFunctions
Functions
 
Chap 9(functions)
Chap 9(functions)Chap 9(functions)
Chap 9(functions)
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
 
Functions
FunctionsFunctions
Functions
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
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
 
10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 

More from monikadeshmane

More from monikadeshmane (20)

File system node js
File system node jsFile system node js
File system node js
 
Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modules
 
Nodejs buffers
Nodejs buffersNodejs buffers
Nodejs buffers
 
Intsllation & 1st program nodejs
Intsllation & 1st program nodejsIntsllation & 1st program nodejs
Intsllation & 1st program nodejs
 
Nodejs basics
Nodejs basicsNodejs basics
Nodejs basics
 
Chap 5 php files part-2
Chap 5 php files   part-2Chap 5 php files   part-2
Chap 5 php files part-2
 
Chap 5 php files part 1
Chap 5 php files part 1Chap 5 php files part 1
Chap 5 php files part 1
 
Chap4 oop class (php) part 2
Chap4 oop class (php) part 2Chap4 oop class (php) part 2
Chap4 oop class (php) part 2
 
Chap4 oop class (php) part 1
Chap4 oop class (php) part 1Chap4 oop class (php) part 1
Chap4 oop class (php) part 1
 
Chap 3php array part4
Chap 3php array part4Chap 3php array part4
Chap 3php array part4
 
Chap 3php array part 3
Chap 3php array part 3Chap 3php array part 3
Chap 3php array part 3
 
Chap 3php array part 2
Chap 3php array part 2Chap 3php array part 2
Chap 3php array part 2
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
 
php string part 4
php string part 4php string part 4
php string part 4
 
php string part 3
php string part 3php string part 3
php string part 3
 
php string-part 2
php string-part 2php string-part 2
php string-part 2
 
PHP string-part 1
PHP string-part 1PHP string-part 1
PHP string-part 1
 
java script
java scriptjava script
java script
 
ip1clientserver model
 ip1clientserver model ip1clientserver model
ip1clientserver model
 
Chap1introppt2php(finally done)
Chap1introppt2php(finally done)Chap1introppt2php(finally done)
Chap1introppt2php(finally done)
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 

PHP function

  • 1. Chapter 2- Functions & Strings (Part 1) Functions 1
  • 2. Functions 2. Function and String 2.1Defining and calling a function 2.2 Default parameters 2.3 Variable parameters, Missing parameters 2.4 Variable function, Anonymous function 2
  • 3. Functions Defining a function function [&] function_name([param[,…]]) { //code } Function name are case insensitive. If the function is defined with the optional ampersand then that function returns a reference to the returned data. 3
  • 4. Revise – • Function types- 1. Built-in 2. User defined • calling to function(return or no return) • Call by value - f($a); • Call by reference - &f($a); Ex.function & f($a) { } $v=& f(10); 4
  • 5. Functions :Parameter types 1. Default Parameters 2. Variable Parameters 3. Missing Parameters 5
  • 6. Functions :Parameter types 1. Default Parameters function pival($pi=3.14) { echo “The value of PI is $pi”; } The default value is constant. While calling function, pival(); // The value of PI is 3.14 pival(null); // The value of PI is pival(3.00); // The value of PI is 3.00 A function may have any number of parameters with default values, but they must be listed after all the parameters. 6
  • 7. Functions <?php function display($types = array("C lang"), $author = NULL) { echo "$types[0]<br/>"; echo $author; } display(); display(array("CPP lang", "Java lang"), "ABC"); ?> 7
  • 8. Functions :Parameter types 2. Variable Parameters When number of parameters are not fixed then Variable Parameter function can be used. For e.g. to find out area of different shapes. PHP provides three functions: 1. func_get_args(): It returns an array of all parameter provided to the function. 2. func_num_args(): It returns the number of parameters provided to the function. 3. func_get_arg(): It returns a specific argument from the parameters. 8
  • 9. Example- Variable Parameters <?php function area() { if (func_num_args() == 0) { return false; } for ($i = 0; $i < func_num_args(); $i++) { $variables += func_get_arg($i); } return $variables; } echo area(20,20,10,40); ?> 9
  • 10. Functions :Parameter types 3. Missing Parameters When you call a function, you can pass any number of arguments to the function. Any parameters the function expects that are not passed to it remain unset, and a warning is issued for each of missing parameter. 10
  • 11. Example- Missing Parameters <?php function display($a, $b) { if (isset($a)) { echo "first argument value is set"; } if (isset($b)) { echo "second argument value is set"; } }display(10,23); display(20); display();?> 11
  • 12. Function Type-1. Variable Function As with variable variables, you can call a function based on the value of a variable. For e.g., a variable is used to determine which of three functions to call: switch($which) { case 'first': first( ); break; case 'second': second( ); break; case 'third': third( ); break; } In this case, we could use a variable function call to call the appropriate function. To make a variable function call, include the parameters for a function in parentheses after the variable. $f = 'echo'; $f('hello, world'); // does not work 12
  • 13. Variable Function To rewrite the previous example: $which(); // if $which is "first" the function first( ) is called, etc... If no function exists for the variable, a runtime error occurs when the code is evaluated. So, you can use the built-in function function_exists( ) to check whether a function exists for the value of the variable before calling the function: $yes_or_no = function_exists(function_name); For example: if(function_exists($which)) { $which(); // if $which is "first" the function first( ) is called, etc... } 13
  • 14. 14 <?php $which='first'; switch($which) { //case 'first': first( ); break; case 'second': second( ); break; case 'third': third( ); break; } if(function_exists($which)) { $which(); } function first() { echo "hiii"; } ?>
  • 15. Functions Type- 2. Anonymous Function These functions are localized and temporary & with no names. You can create an anonymous function using create_function( ). This function takes two parameters-the first shows the parameters the anonymous function takes in, and the second is the actual code or functionality. A randomly generated name for the function is returned: $func_name=create_function(args_string,code_string) 15
  • 16. Example Anonymous Function These functions are <?php $anonymous_function = create_function('$a, $b', 'return $a*$b;'); print $anonymous_function(3,7); ?> 16