SlideShare una empresa de Scribd logo
1 de 18
Descargar para leer sin conexión
Php MySql part -3
    Subhasis Nayak

   CMC
Contents
    Using functions
    Defining functions
    Arguments & parameters
    Default argument values
    Variable scope
    Using library files
    Including library files
Using functions
 Functions are used to do certain actions. It consists block of code.
    When we call a function it runs the code and give a result,
   It may takes outside values.
   It may return values to outside.
   When we call a function it jumps to the definition of that function
    and start processing codes which are inside of the ―{}‖ and once
    completes it jumps back to the next line from where it was called.
   PHP contains many wide range built in functions which can be
    used to do solve many tasks.
User defined function
   We will put some reusable code into function and use it again
    and again. Here we can change the function if we want to
    change how does it work or processing the block of code.
   We can maintain our code easily and quickly.
   Reduce the duplication of code.
   Reduce the effort and time.
   Changing the function definition effect all over where it is
    called.
Defining function
 In php to define function we need the key word ―function‖
  and then name of the function.
 Here we do not need any return type. What ever we
  returned by default it will get it’s data type.
 But when we want to return from the function we have to
  use keyword ‖return‖.

               function add(int x, int y){
               return (x+y);
               }
An example.
Arguments & parameters
   When we define the function we used variables with in the
    first bracket.
   On that time those variables are known as parameter lists.
   When we call function using variables those are called
    arguments.
   Well, arguments are used to pass the outside variables as local
    variables of local variables of function.
   If we want to use GLOBAL variable use GLOBAL keyword
    before it.
An example
Default arguments
 Let’s we use 2 – parameters. If we do not pass two arguments
  we will get an error.
 In complex logic sometimes we do not need all the
  arguments. On this case what will we do.
 Thanks to PHP it provides us a feature through which we can
  make our arguments default.
 To write a default argument, we have to initialize the
  parameters when we use them on first bracket. Let’s see.
Cont’d …..
function myFunc($a=0){
                         As I am not passing
Return a;                any argument it will
}                          take default one.
echo myFunc();

function myFunc($a){
                          As I am not passing
Return a;                 any argument it will
}                            give an error.
echo myFunc();
Variable scope
 The reason values have to be passed in to functions as
  arguments has to do with variable scope—the rules that
  determine what sections of script are able to access which
  variables.
 The basic rule is that any variables are:
     Variable defined in the main body of the script cannot be used
      inside a function.
     any variables used inside a function cannot be seen by the main
      script.
Cont’d ……
 Local – variables within a function are said to be local
  variables or that their scope is local to that function.
 Global - Variables that are not local are called global
  variables.
 Local and global variables can have the same name and
  contain different values.
 To access global variable inside the function use keyword
  ‖global‖ as prefix of the variable
An example

             Give output 250




              Give an error
Using library files
 If we want to use it again in other scripts. Rather than copy
  the function definition into each script that needs to use it,
  you can use a library file so that your function needs to be
  stored and maintained in only one place.
 A library file needs to enclose its PHP code inside <?php tags
  just like a regular script; otherwise, the contents will be
  displayed as HTML when they are included in a script.
 To do so create a ―.php‖ file put your all functions.
Including library files
 To incorporate an external library file into another script,
  you use the include keyword.
 The include path Setting By default, include searches only the
  current directory and a few system locations for files to be
  included.
 If you want to include files from another location, you can
  use a path to the file.
 You can use the include_once keyword if you want to
  make sure that a library file is loaded only once.
Cont’d …..
 If a script attempts to define the same function a second
  time, an error will result.
 Using include_once helps to avoid this, particularly when
  files are being included from other library files.let’ds do it
  practically.
An example
   I write a function ―add_tax‖ in ―addTax.php‖
Next write a file “useTax.php”
   Use include_once/include to add like below:
     include_once "addTax.php";
   Then call the function “add_tax()” any where in
    your block. I used in this way.

Más contenido relacionado

La actualidad más candente

Functions in python
Functions in pythonFunctions in python
Functions in pythoncolorsof
 
Introduction to php 5
Introduction to php   5Introduction to php   5
Introduction to php 5pctechnology
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes NUST Stuff
 
Python Function and Looping
Python Function and LoopingPython Function and Looping
Python Function and LoopingNovita Sari
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript FundamentalsSrdjan Strbanovic
 
Command line arguments.21
Command line arguments.21Command line arguments.21
Command line arguments.21myrajendra
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascriptrelay12
 
Write codeforhumans
Write codeforhumansWrite codeforhumans
Write codeforhumansNarendran R
 
Java Script Language Tutorial
Java Script Language TutorialJava Script Language Tutorial
Java Script Language Tutorialvikram singh
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpuDhaval Jalalpara
 

La actualidad más candente (20)

Java script function
Java script functionJava script function
Java script function
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Introduction to php 5
Introduction to php   5Introduction to php   5
Introduction to php 5
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Functions
FunctionsFunctions
Functions
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
Python and You Series
Python and You SeriesPython and You Series
Python and You Series
 
Books
BooksBooks
Books
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
 
Python Function and Looping
Python Function and LoopingPython Function and Looping
Python Function and Looping
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
 
Command line arguments.21
Command line arguments.21Command line arguments.21
Command line arguments.21
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
 
Write codeforhumans
Write codeforhumansWrite codeforhumans
Write codeforhumans
 
Java Script Language Tutorial
Java Script Language TutorialJava Script Language Tutorial
Java Script Language Tutorial
 
Inline function
Inline functionInline function
Inline function
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 

Similar a PHP MySQL Part 3 - Functions, Arguments, Scopes, Libraries

Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in phpAshish Chamoli
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfSudhanshiBakre1
 
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).pptxManas40552
 
Functions in Python Syntax and working .
Functions in Python Syntax and working .Functions in Python Syntax and working .
Functions in Python Syntax and working .tarunsharmaug23
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdfalaparthi
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfDeeptiMalhotra19
 
Dynamic website
Dynamic websiteDynamic website
Dynamic websitesalissal
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1YOGESH SINGH
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptxAshwini Raut
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAMAN ANAND
 
Functions and Arguments in Python
Functions and Arguments in PythonFunctions and Arguments in Python
Functions and Arguments in PythonMars Devs
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptxzueZ3
 

Similar a PHP MySQL Part 3 - Functions, Arguments, Scopes, Libraries (20)

Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in php
 
Dive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdfDive into Python Functions Fundamental Concepts.pdf
Dive into Python Functions Fundamental Concepts.pdf
 
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
 
Functions in Python Syntax and working .
Functions in Python Syntax and working .Functions in Python Syntax and working .
Functions in Python Syntax and working .
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
 
Python-Functions.pptx
Python-Functions.pptxPython-Functions.pptx
Python-Functions.pptx
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
 
UNIT 3 python.pptx
UNIT 3 python.pptxUNIT 3 python.pptx
UNIT 3 python.pptx
 
Dynamic website
Dynamic websiteDynamic website
Dynamic website
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
4. function
4. function4. function
4. function
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
 
Python functions
Python functionsPython functions
Python functions
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
functionnotes.pdf
functionnotes.pdffunctionnotes.pdf
functionnotes.pdf
 
Functions and Arguments in Python
Functions and Arguments in PythonFunctions and Arguments in Python
Functions and Arguments in Python
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 

Más de Subhasis Nayak

Más de Subhasis Nayak (20)

Php, mysq lpart5(mysql)
Php, mysq lpart5(mysql)Php, mysq lpart5(mysql)
Php, mysq lpart5(mysql)
 
working with database using mysql
working with database using mysql working with database using mysql
working with database using mysql
 
Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)
 
Jsp 01
Jsp 01Jsp 01
Jsp 01
 
Jsp 02(jsp directives)2003
Jsp 02(jsp directives)2003Jsp 02(jsp directives)2003
Jsp 02(jsp directives)2003
 
Php, mysq lpart1
Php, mysq lpart1Php, mysq lpart1
Php, mysq lpart1
 
Php, mysqlpart2
Php, mysqlpart2Php, mysqlpart2
Php, mysqlpart2
 
C:\fakepath\jsp01
C:\fakepath\jsp01C:\fakepath\jsp01
C:\fakepath\jsp01
 
Servlet & jsp
Servlet  &  jspServlet  &  jsp
Servlet & jsp
 
J2ee connector architecture
J2ee connector architectureJ2ee connector architecture
J2ee connector architecture
 
how to create object
how to create objecthow to create object
how to create object
 
Pointer in c++ part3
Pointer in c++ part3Pointer in c++ part3
Pointer in c++ part3
 
Pointer in c++ part2
Pointer in c++ part2Pointer in c++ part2
Pointer in c++ part2
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 
C++ arrays part2
C++ arrays part2C++ arrays part2
C++ arrays part2
 
C++ arrays part1
C++ arrays part1C++ arrays part1
C++ arrays part1
 
Introduction to network
Introduction to networkIntroduction to network
Introduction to network
 
Flow control in c++
Flow control in c++Flow control in c++
Flow control in c++
 
Oops And C++ Fundamentals
Oops And C++ FundamentalsOops And C++ Fundamentals
Oops And C++ Fundamentals
 
Text mode Linux Installation Part 01
Text mode Linux Installation Part 01Text mode Linux Installation Part 01
Text mode Linux Installation Part 01
 

Último

Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 

Último (20)

Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 

PHP MySQL Part 3 - Functions, Arguments, Scopes, Libraries

  • 1. Php MySql part -3 Subhasis Nayak CMC
  • 2. Contents Using functions Defining functions Arguments & parameters Default argument values Variable scope Using library files Including library files
  • 3. Using functions  Functions are used to do certain actions. It consists block of code. When we call a function it runs the code and give a result,  It may takes outside values.  It may return values to outside.  When we call a function it jumps to the definition of that function and start processing codes which are inside of the ―{}‖ and once completes it jumps back to the next line from where it was called.  PHP contains many wide range built in functions which can be used to do solve many tasks.
  • 4. User defined function  We will put some reusable code into function and use it again and again. Here we can change the function if we want to change how does it work or processing the block of code.  We can maintain our code easily and quickly.  Reduce the duplication of code.  Reduce the effort and time.  Changing the function definition effect all over where it is called.
  • 5. Defining function  In php to define function we need the key word ―function‖ and then name of the function.  Here we do not need any return type. What ever we returned by default it will get it’s data type.  But when we want to return from the function we have to use keyword ‖return‖. function add(int x, int y){ return (x+y); }
  • 7. Arguments & parameters  When we define the function we used variables with in the first bracket.  On that time those variables are known as parameter lists.  When we call function using variables those are called arguments.  Well, arguments are used to pass the outside variables as local variables of local variables of function.  If we want to use GLOBAL variable use GLOBAL keyword before it.
  • 9. Default arguments  Let’s we use 2 – parameters. If we do not pass two arguments we will get an error.  In complex logic sometimes we do not need all the arguments. On this case what will we do.  Thanks to PHP it provides us a feature through which we can make our arguments default.  To write a default argument, we have to initialize the parameters when we use them on first bracket. Let’s see.
  • 10. Cont’d ….. function myFunc($a=0){ As I am not passing Return a; any argument it will } take default one. echo myFunc(); function myFunc($a){ As I am not passing Return a; any argument it will } give an error. echo myFunc();
  • 11. Variable scope  The reason values have to be passed in to functions as arguments has to do with variable scope—the rules that determine what sections of script are able to access which variables.  The basic rule is that any variables are:  Variable defined in the main body of the script cannot be used inside a function.  any variables used inside a function cannot be seen by the main script.
  • 12. Cont’d ……  Local – variables within a function are said to be local variables or that their scope is local to that function.  Global - Variables that are not local are called global variables.  Local and global variables can have the same name and contain different values.  To access global variable inside the function use keyword ‖global‖ as prefix of the variable
  • 13. An example Give output 250 Give an error
  • 14. Using library files  If we want to use it again in other scripts. Rather than copy the function definition into each script that needs to use it, you can use a library file so that your function needs to be stored and maintained in only one place.  A library file needs to enclose its PHP code inside <?php tags just like a regular script; otherwise, the contents will be displayed as HTML when they are included in a script.  To do so create a ―.php‖ file put your all functions.
  • 15. Including library files  To incorporate an external library file into another script, you use the include keyword.  The include path Setting By default, include searches only the current directory and a few system locations for files to be included.  If you want to include files from another location, you can use a path to the file.  You can use the include_once keyword if you want to make sure that a library file is loaded only once.
  • 16. Cont’d …..  If a script attempts to define the same function a second time, an error will result.  Using include_once helps to avoid this, particularly when files are being included from other library files.let’ds do it practically.
  • 17. An example  I write a function ―add_tax‖ in ―addTax.php‖
  • 18. Next write a file “useTax.php”  Use include_once/include to add like below:  include_once "addTax.php";  Then call the function “add_tax()” any where in your block. I used in this way.