IS 118 Introduction to Development Tools - PowerPoint PPT Presentation

About This Presentation
Title:

IS 118 Introduction to Development Tools

Description:

IS 118 Introduction to Development Tools Chapter 5 Reusing Code Things to Cover Why reuse code require() include() Variations Why Reuse Code? Lower Cost The cost of ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 16
Provided by: me6105
Learn more at: https://web.njit.edu
Category:

less

Transcript and Presenter's Notes

Title: IS 118 Introduction to Development Tools


1
IS 118 Introduction to Development Tools
  • Chapter 5
  • Reusing Code

2
Things to Cover
  • Why reuse code
  • require()
  • include()
  • Variations

3
Why Reuse Code?
  • Lower Cost
  • The cost of writing code is high
  • Time to write
  • Time to check and debug
  • Time to document and implement
  • Cost of buying existing code is cheaper than
    writing it
  • Reliability
  • Normally if code is already in use it works!
  • Consistency
  • Its already done with the look we want

4
require()
  • reusable.php
  • lt?php
  • echo Here is a simple php statement.ltbr /gt
  • ?gt
  • To use code in main.php
  • lt?php
  • echo 'This is the main file.ltbr /gt'
  • require( 'reusable.php' )
  • echo 'The script will end now.ltbr /gt'
  • ?gt

5
Require() - 2
  • This is the same as writing
  • lt?php
  • echo 'This is the main file.ltbr /gt'
  • echo Here is a simple php statement.ltbr /gt
  • echo 'The script will end now.ltbr /gt'
  • ?gt

6
Require() -3
  • Warnings
  • Require just sticks the code in, if it is php it
    will be executed!
  • Could idea not to use other extensions
  • But maybe .inc to help identify it
  • Need to use php tags, i.e.
  • lt?php ?gt

7
Why use
  • Website templates
  • See home.html and home.php on CD

8
Calling Functions
  • Functions are like reusable code
  • Have already been written
  • Been debugged and documented
  • They work
  • Simple way to call
  • function_name()
  • phpinfo() info on installed version

9
Calling Functions
  • Prototype
  • A prototype is a model of how to use the function
  • Resource fopen( string filename, string mode
  • , bool use_include_path , resource zcontext)
  • NOTE function names are NOT case sensitive
  • Name must be unique
  • The prototype defines the parameters to be passed

10
Parameters
  • Resource fopen( string filename, string mode ,
    bool use_include_path , resource zcontext)
  • The first two parms are required
  • The next two are optional
  • Optional values some or all can be provided

11
Scope
  • The scope is where a variable is visible and
    usable
  • Declared inside a function only within that
    function (local variable)
  • Declared outside a function available
    everywhere EXCEPT the function (global variables)
  • Superglobal variables available everywhere
    including functions (chapter 1)

12
Scope 2
  • Can use keyword global to make a variable within
    a function have global scope
  • Can delete a variable by using unset()
  • Unset ( variable_name)
  • It is no longer available in any scope

13
Scope 3 pg 149
  • Scope causes problems within functions
  • Function increment( value, amount 1)
  • value value amount
  • Does not work because you can not change the
    value of value not in scope!
  • Have to pass value by reference
  • function increment( value, amount 1)
  • This works

14
Returning values
  • With in a function can use a return statement to
    end execution of it
  • But it also can return a value
  • Pg 151 return x, return false
  • Use as such
  • echo larger (a, b).ltbr /gt
  • The function returns the larger vale and it is
    printed

15
Recursion
  • Recursion is a function that calls itself
  • Not used often is web based application
  • Used instead of iteration
Write a Comment
User Comments (0)
About PowerShow.com