SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Downloaden Sie, um offline zu lesen
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.

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von Subhasis Nayak

Mehr von 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
 

Kürzlich hochgeladen

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
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
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
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
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 

Kürzlich hochgeladen (20)

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
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
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
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
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 

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.