1 / 23

IT420: Database Management and Organization

Learn how to write reusable code in PHP, organize your database, and manage external files. Includes functions, variables scope, and user-defined functions.

mcade
Download Presentation

IT420: Database Management and Organization

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. IT420: Database Management and Organization PHP –Writing Reusable Code 10 March 2006 Adina Crainiceanu www.cs.usna.edu/~adina

  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: <html> <head> <title> My Page Name </title> </head> <body bgcolor=“#FFFFF”> • 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 <html> <head> <title> <?php echo $pageTitle; ?> </title> </head> <body bgcolor=“#FFFFF”>

  7. Include example (cont) • index.php <?php $pageTitle = “Adina’s Page”; include (“header.php”); echo “Some content for my page”; ?>

  8. Generated HTML Page <html> <head> <title> Adina’s Page </title> </head> <body bgcolor=“#FFFFF”> 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 <?php my_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 <br/>’; echo $var; function fn(){ echo $var; $var = ‘content 2 <br/>’; 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

More Related