SlideShare a Scribd company logo
1 of 18
Download to read offline
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.

More Related Content

What's hot

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
 

What's hot (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 to 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 to 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
 

More from Subhasis Nayak

More from 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
 

Recently uploaded

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 

Recently uploaded (20)

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 

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.