1 / 15

IS 118 Introduction to Development Tools

Learn why reusing code is beneficial, how to use require() and include(), and the advantages of using website templates and calling functions. Understand scope, parameters, and returning values in PHP.

castillom
Download Presentation

IS 118 Introduction to Development Tools

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  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 IS 118

  4. require() • reusable.php • <?php • echo ‘Here is a simple php statement.<br />; • ?> • To use: code in main.php • <?php • echo 'This is the main file.<br />'; • require( 'reusable.php' ); • echo 'The script will end now.<br />'; • ?> IS 118

  5. Require() - 2 • This is the same as writing: • <?php • echo 'This is the main file.<br />'; • echo ‘Here is a simple php statement.<br />; • echo 'The script will end now.<br />'; • ?> IS 118

  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. • <?php … ?> IS 118

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

  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 IS 118

  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 IS 118

  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 IS 118

  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) IS 118

  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 IS 118

  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 IS 118

  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).’<br />’; • The function returns the larger vale and it is printed IS 118

  15. Recursion • Recursion is a function that calls itself • Not used often is web based application • Used instead of iteration IS 118

More Related