IT420: Database Management and Organization –Writing Reusable Code PHP

advertisement
IT420: Database Management
and Organization
PHP –Writing Reusable Code
10 March 2006
Adina Crainiceanu
www.cs.usna.edu/~adina
Goals Today
 Writing Reusable code
 External files
 Functions
Typical Function Presentation
 string date (string format [, int timestamp])
Return type
Function name
Parameter type
Parameter name
Optional
parameter(s)
Calling a Function
 $todayDate = date(“d F y”);
 Note:
 Function names are NOT case sensitive
 Variable names are case senstive
Writing Reusable Code
 Example: Typical page header:
<html>
<head>
<title> My Page Name </title>
</head>
<body bgcolor=“#FFFFF”>
 Type same code for every page?
 What if want to change bgcolor?
Include files
 Write code once and save it into a file
 Include the file in every page
 Example: header.php
<html>
<head>
<title> <?php echo $pageTitle; ?> </title>
</head>
<body bgcolor=“#FFFFF”>
Include example (cont)
 index.php
<?php
$pageTitle = “Adina’s Page”;
include (“header.php”);
echo “Some content for my page”;
?>
Generated HTML Page
<html>
<head>
<title> Adina’s Page </title>
</head>
<body bgcolor=“#FFFFF”>
Some content for my page
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)
Class Exercise
 Write the code for a footer in a file
 Include it in index.php file, to obtain a wellformed HTML document
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
…
Define a Function
 function my_function() {
echo ‘This is printed by my function’;
}
Keyword to define a
function
Function name
Function code
Calling the Function
<?php my_function(); ?>
Result:
This is printed by my function
Function Parameters
 function my_function($text) {
echo $text;
}
 Call: my_function(“Print this text”);
 Result: Print this text
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
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!
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
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
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
Variables Scope Example
 function fn(){
$var = ‘content’;
}
fn();
echo $var;
 Result?
 Nothing is printed!
Variables Scope Example 2
$var = ‘content 1 <br/>’;
echo $var;
function fn(){
echo $var;
$var = ‘content 2 <br/>’;
echo $var
}
fn();
echo $var;
 Result?
content 1
content 2
content 1
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
Class Exercise
 Write the PHP script to use my_dbconnect
 Connect to localhost
 Default user and password
 Select database vp5fund
Download