150 likes | 163 Views
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.
E N D
IS 118 Introduction to Development Tools Chapter 5 Reusing Code IS 118
Things to Cover • Why reuse code • require() • include() • Variations IS 118
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
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
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
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
Why use • Website templates • See home.html and home.php on CD IS 118
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
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
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
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
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
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
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
Recursion • Recursion is a function that calls itself • Not used often is web based application • Used instead of iteration IS 118