PHP - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

PHP

Description:

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 ... – PowerPoint PPT presentation

Number of Views:208
Avg rating:3.0/5.0
Slides: 24
Provided by: thomasm1
Category:
Tags: php | leap

less

Transcript and Presenter's Notes

Title: PHP


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
  • lthtmlgt
  • ltheadgt
  • lttitlegt My Page Name lt/titlegt
  • lt/headgt
  • ltbody bgcolorFFFFFgt
  • 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
  • lthtmlgt
  • ltheadgt
  • lttitlegt lt?php echo pageTitle ?gt lt/titlegt
  • lt/headgt
  • ltbody bgcolorFFFFFgt

7
Include example (cont)
  • index.php
  • lt?php
  • pageTitle Adinas Page
  • include (header.php)
  • echo Some content for my page
  • ?gt

8
Generated HTML Page
  • lthtmlgt
  • ltheadgt
  • lttitlegt Adinas Page lt/titlegt
  • lt/headgt
  • ltbody bgcolorFFFFFgt
  • 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
  • lt?php my_function() ?gt
  • 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(textDefault 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, cellspacing2,
    cellpadding2)
  • echo lttable border border
  • cellspacing cellspacing
  • cellpadding cellpaddinggt
  • 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 ltbr/gt
  • echo var
  • function fn()
  • echo var
  • var content 2 ltbr/gt
  • 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
Write a Comment
User Comments (0)
About PowerShow.com