Download presentation
Presentation is loading. Please wait.
1
IS 1181 IS 118 Introduction to Development Tools Chapter 5 Reusing Code
2
IS 1182 Things to Cover Why reuse code require() include() Variations
3
IS 1183 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
IS 1184 require() reusable.php <?php echo ‘Here is a simple php statement. ; ?> To use: code in main.php <?php echo 'This is the main file. '; require( 'reusable.php' ); echo 'The script will end now. '; ?>
5
IS 1185 Require() - 2 This is the same as writing: <?php echo 'This is the main file. '; echo ‘Here is a simple php statement. ; echo 'The script will end now. '; ?>
6
IS 1186 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.
7
IS 1187 Why use Website templates See home.html and home.php on CD
8
IS 1188 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
IS 1189 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
IS 11810 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
IS 11811 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
IS 11812 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
IS 11813 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
IS 11814 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).’ ’; The function returns the larger vale and it is printed
15
IS 11815 Recursion Recursion is a function that calls itself Not used often is web based application Used instead of iteration
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.