Download presentation
Presentation is loading. Please wait.
Published byEthel Watts Modified over 9 years ago
1
PHP –Writing Reusable Code 10 March 2006 Adina Crainiceanu www.cs.usna.edu/~adina IT420: Database Management and Organization
2
Goals Today Writing Reusable code External files Functions
3
Typical Function Presentation string date (string format [, int timestamp]) Return type Function name Parameter type Parameter name Optional parameter(s)
4
Calling a Function $todayDate = date(“d F y”); Note: Function names are NOT case sensitive Variable names are case senstive
5
Writing Reusable Code Example: Typical page header: My Page Name Type same code for every page? What if want to change bgcolor?
6
Include files Write code once and save it into a file Include the file in every page Example: header.php
7
Include example (cont) index.php <?php $pageTitle = “Adina’s Page”; include (“header.php”); echo “Some content for my page”; ?>
8
Generated HTML Page Adina’s Page Some content for my page
9
Include Functions include(string fileName) Includes the content of fileName into current file require(string fileName) Like include() Fatal error if fileName not found! include_once(string fileName) require_once(string fileName)
10
Class Exercise Write the code for a footer in a file Include it in index.php file, to obtain a well- formed HTML document
11
User-Defined Functions Define functions to performs repetitive tasks Examples: Open a database connection and select a database Display the elements of an array as a table …
12
Define a Function function my_function() { echo ‘This is printed by my function’; } Keyword to define a function Function name Function code
13
Calling the Function Result: This is printed by my function
14
Function Parameters function my_function($text) { echo $text; } Call: my_function(“Print this text”); Result: Print this text
15
Optional Parameters function my_function($text=“Default text”) { echo $text; } Call: my_function(“Print this text”); Result: Print this text Call: my_function(); Result: Default text
16
Multiple Optional Parameters function start_table($border, $cellspacing=2, $cellpadding=2){ echo “<table border = $border cellspacing = $cellspacing cellpadding = $cellpadding>”; } start_table(1) equivalent start_table(1,2,2) start_table(2,3) equivalent start_table(2,3,2) start_table(2,3,4) Parameter values filled in from left to right!
17
Return Values function add_values($a, $b){ $result = $a + $b; return $result; } Call: $added_val = add_values(4,5); Result: $added_val has value 9
18
Class Exercise Write a function is_leap to test whether a yar is a leap year or not Return “Yes” if input parameter is leap year Return “No” if input parameter is not a leap year
19
Variables Scope Variables declared in functions are visible from declaration line to end of function – local variables Variables declared outside functions are visible from declaration line to end of file, but not inside functions – global variables Superglobal variables ($_POST, $_SERVER, …) are visible everywhere Keyword global makes local variables global – not recommended
20
Variables Scope Example function fn(){ $var = ‘content’; } fn(); echo $var; Result? Nothing is printed!
21
Variables Scope Example 2 $var = ‘content 1 ’; echo $var; function fn(){ echo $var; $var = ‘content 2 ’; echo $var } fn(); echo $var; Result? content 1 content 2 content 1
22
Class Exercise Write a function my_dbconnect to open a db connection and select a database Input parameters: db server name user name – optional, default value “root” password – optional, default value “” database name Return value: FALSE if errors occurred database connection if everything OK
23
Class Exercise Write the PHP script to use my_dbconnect Connect to localhost Default user and password Select database vp5fund
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.