SlideShare a Scribd company logo
1 of 14
function
PAPER NAME : PROGRAMMING IN PHP
STAFF NAME : MS. V. SHANTHI M.sc, M.phil,M.tech,PGDAS.
CLASS : III BCA
SEMESTER : VI
Why Functions?
 PHP has stacks of built-in functions that we use all
the time.
 We write our own functions when our code spreads
a certain level of difficulty.
echo strrev(" .dlrow olleH"); echo str_repeat("Hip ", 2);
echo strtoupper("hooray!");
echo strlen("intro");
echo "n";
Hello world.
Hip Hip
HOORAY!
5
OUTPUT
Built-InFunctions...
 Much of the influence of PHP comes from its built-in
functions.
 Many are modeled after C string collection functions
(i.e. strlen()).
echo strrev(" .dlrow olleH"); echo str_repeat("Hip ", 2); echo
strtoupper("hooray!");
echo strlen("intro");
echo "n";
Hello world.
Hip Hip
HOORAY!
5
OUTPUT
 Functions can choose to receive optional influences.
Within the function definition the variable names are
successfully “aliases” to the values passed in when the
function is called.
Arguments
function howdy($lang) {
if ( $lang == 'es' ) return "Hola";
if ( $lang == 'fr' ) return "Bonjour";
return "Hello";
}
print howdy('es') . " Glennn";
print howdy('fr') . " Sallyn";
Hola Glenn
Bonjour Sally
OUTPUT
Default arguments
 No args passed would mean using the defaulting
values
 Sometimes useful
 Again, nothing about types...
<?php
function myfunction1($arg1="D"){
echo $arg1 . "<br>";
}
myfunction1("bla bla bla");
$strA="A";
myfunction1($strA);
$intA=12;
myfunction1($intA);
myfunction1();
?>
Call By Value
 The quarrel variable within the function is an
“alias” to the real variable.
 But even advance, the alias is to a *copy* of the
real variable in the function call.
function double($alias) {
$alias = $alias * 2;
return $alias;
}
$val = 10;
$dval = double($val);
echo "Value = $val Doubled = $dvaln";
Value = 10 Doubled = 20
OUTPUT
Call By Reference
 Sometimes we want a function to change one of
its influences, so we indicate that an quarrel is
“by reference” using ( & ).
function triple(&$realthing) {
$realthing = $realthing * 3;
}
$val = 10;
triple($val);
echo "Triple = $valn";
Triple = 30
OUTPUT
Recursive functions
 Be enormously careful as the program might
not stop calling itself!!
Avoid recursive function/method calls with over 100-
200 recursion levels as it can smash the stack and
cause a dissolution of the present script.
<?php
function recur($intN){
if ($intN ==1)
return "this is a power of 2<br>";
elseif ($intN%2 == 1)
return "not a power of 2<br>";
else {
$intN /=2;
return recur($intN);
}
}
echo "256: " . recur(256);
echo "1024: " . recur(1024);
echo "1025: " . recur(1025);
?>
Variable Scope
 In general, variable names used privileged of function
code do not mix with the variables external of the
function to evade unexpected side effects if two
programmers use the similar variable name in
dissimilar parts of the code.
 We call this “name spacing” the variables. The
function variables are in one “name space” whilst the
main variables are in additional “name space”.
http://php.net/manual/en/language.variable
s.scope.php
 Any legal PHP code may seem inside a function,
even other functions and class explanations.
 A legal function name twitches with a letter or
underscore, followed by any number of letters,
numbers, or unrscores.
 PHP does not sustenance function overloading,
nor is it possible to undefine or redefine formerly-
professed functions.
 Both variable number of arguments and
avoidance arguments are maintained in functions.
User-definedFunctions
 All functions besides classes in PHP have the global
scope.
 Functions need not be defined before they are
referenced, excepting when a function is conditionally
demarcated as shown.
User-definedFunctions
Date/time functions
 $arrMyDate = getdate();
 $intSec = $arrMyDate['seconds'];
 $intMin = $arrMyDate['minutes'];
 $intHours = $arrMyDate['hours'];
 Etc, e.g., ints 'mday', 'wday', 'mon', 'year',
'yday'
 Strings 'weekday', 'month'

More Related Content

What's hot

What's hot (19)

Php5 certification mock exams
Php5 certification mock examsPhp5 certification mock exams
Php5 certification mock exams
 
Php operators
Php operatorsPhp operators
Php operators
 
Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
 
外傷的Elixir
外傷的Elixir外傷的Elixir
外傷的Elixir
 
week-15x
week-15xweek-15x
week-15x
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
 
PHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP LimogesPHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP Limoges
 
Javascript - The basics
Javascript - The basicsJavascript - The basics
Javascript - The basics
 
Functional php
Functional phpFunctional php
Functional php
 
Git avançado
Git avançadoGit avançado
Git avançado
 
Data Types Master
Data Types MasterData Types Master
Data Types Master
 
Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php
 
Richard practica nº2.php
Richard  practica nº2.phpRichard  practica nº2.php
Richard practica nº2.php
 
C to perl binding
C to perl bindingC to perl binding
C to perl binding
 
Alex Troush - IEx Cheat Sheet
Alex Troush - IEx Cheat Sheet Alex Troush - IEx Cheat Sheet
Alex Troush - IEx Cheat Sheet
 
Compiladoresemulador
CompiladoresemuladorCompiladoresemulador
Compiladoresemulador
 
print even or odd number in c
print even or odd number in cprint even or odd number in c
print even or odd number in c
 
Applications of list
Applications of listApplications of list
Applications of list
 

Similar to PHP functions and scopes explained

Similar to PHP functions and scopes explained (20)

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
 
PHP - Web Development
PHP - Web DevelopmentPHP - Web Development
PHP - Web Development
 
Powershell notes
Powershell notesPowershell notes
Powershell notes
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 
Training on php by cyber security infotech (csi)
Training on  php by cyber security infotech (csi)Training on  php by cyber security infotech (csi)
Training on php by cyber security infotech (csi)
 
null Pune meet - Application Security: Code injection
null Pune meet - Application Security: Code injectionnull Pune meet - Application Security: Code injection
null Pune meet - Application Security: Code injection
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
PHP - Introduction to PHP Functions
PHP -  Introduction to PHP FunctionsPHP -  Introduction to PHP Functions
PHP - Introduction to PHP Functions
 
PHP Reviewer
PHP ReviewerPHP Reviewer
PHP Reviewer
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Javascript fundamentals for php developers
Javascript fundamentals for php developersJavascript fundamentals for php developers
Javascript fundamentals for php developers
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
Day1
Day1Day1
Day1
 

Recently uploaded

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
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
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
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
_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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Recently uploaded (20)

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
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
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
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
_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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

PHP functions and scopes explained

  • 2. PAPER NAME : PROGRAMMING IN PHP STAFF NAME : MS. V. SHANTHI M.sc, M.phil,M.tech,PGDAS. CLASS : III BCA SEMESTER : VI
  • 3. Why Functions?  PHP has stacks of built-in functions that we use all the time.  We write our own functions when our code spreads a certain level of difficulty. echo strrev(" .dlrow olleH"); echo str_repeat("Hip ", 2); echo strtoupper("hooray!"); echo strlen("intro"); echo "n"; Hello world. Hip Hip HOORAY! 5 OUTPUT
  • 4. Built-InFunctions...  Much of the influence of PHP comes from its built-in functions.  Many are modeled after C string collection functions (i.e. strlen()). echo strrev(" .dlrow olleH"); echo str_repeat("Hip ", 2); echo strtoupper("hooray!"); echo strlen("intro"); echo "n"; Hello world. Hip Hip HOORAY! 5 OUTPUT
  • 5.  Functions can choose to receive optional influences. Within the function definition the variable names are successfully “aliases” to the values passed in when the function is called. Arguments function howdy($lang) { if ( $lang == 'es' ) return "Hola"; if ( $lang == 'fr' ) return "Bonjour"; return "Hello"; } print howdy('es') . " Glennn"; print howdy('fr') . " Sallyn"; Hola Glenn Bonjour Sally OUTPUT
  • 6. Default arguments  No args passed would mean using the defaulting values  Sometimes useful  Again, nothing about types... <?php function myfunction1($arg1="D"){ echo $arg1 . "<br>"; } myfunction1("bla bla bla"); $strA="A"; myfunction1($strA); $intA=12; myfunction1($intA); myfunction1(); ?>
  • 7. Call By Value  The quarrel variable within the function is an “alias” to the real variable.  But even advance, the alias is to a *copy* of the real variable in the function call. function double($alias) { $alias = $alias * 2; return $alias; } $val = 10; $dval = double($val); echo "Value = $val Doubled = $dvaln"; Value = 10 Doubled = 20 OUTPUT
  • 8. Call By Reference  Sometimes we want a function to change one of its influences, so we indicate that an quarrel is “by reference” using ( & ). function triple(&$realthing) { $realthing = $realthing * 3; } $val = 10; triple($val); echo "Triple = $valn"; Triple = 30 OUTPUT
  • 9. Recursive functions  Be enormously careful as the program might not stop calling itself!! Avoid recursive function/method calls with over 100- 200 recursion levels as it can smash the stack and cause a dissolution of the present script.
  • 10. <?php function recur($intN){ if ($intN ==1) return "this is a power of 2<br>"; elseif ($intN%2 == 1) return "not a power of 2<br>"; else { $intN /=2; return recur($intN); } } echo "256: " . recur(256); echo "1024: " . recur(1024); echo "1025: " . recur(1025); ?>
  • 11. Variable Scope  In general, variable names used privileged of function code do not mix with the variables external of the function to evade unexpected side effects if two programmers use the similar variable name in dissimilar parts of the code.  We call this “name spacing” the variables. The function variables are in one “name space” whilst the main variables are in additional “name space”. http://php.net/manual/en/language.variable s.scope.php
  • 12.  Any legal PHP code may seem inside a function, even other functions and class explanations.  A legal function name twitches with a letter or underscore, followed by any number of letters, numbers, or unrscores.  PHP does not sustenance function overloading, nor is it possible to undefine or redefine formerly- professed functions.  Both variable number of arguments and avoidance arguments are maintained in functions. User-definedFunctions
  • 13.  All functions besides classes in PHP have the global scope.  Functions need not be defined before they are referenced, excepting when a function is conditionally demarcated as shown. User-definedFunctions
  • 14. Date/time functions  $arrMyDate = getdate();  $intSec = $arrMyDate['seconds'];  $intMin = $arrMyDate['minutes'];  $intHours = $arrMyDate['hours'];  Etc, e.g., ints 'mday', 'wday', 'mon', 'year', 'yday'  Strings 'weekday', 'month'