1 of 35

Chapter 4: PHP scripting- part 2

  • Built in function in PHP

Communicating multiple web pages or files

Redirecting web page

Formatting an output of string

Terminating a process or web page

Manipulating strings

Generating encoded passwords

2 of 35

Objectives

  1. To learn how to use multiple PHP files
  2. To understand how to use built in function in PHP.
  3. To define constant value and validating values.
  4. To format and manipulating strings

3 of 35

Multiple PHP files

  • Step 1: Universal header and footer in a single file

  • Create a file called header.php. This file will have all of the�header HTML code. You can use FrontPage/Dreamweaver to create the header, but remember to remove the closing </BODY> and </HTML> tags.

<html><head>

<title>UCR Webmaster Support Group</title>�<link rel="stylesheet" type="text/css" href=“mycssfile.css">

</head>�<body>

<table width=80% height=30>

<tr><td>� <div align=center> Page Title </div>�</td></tr></table>

4 of 35

Examples

  • Step 2: Universal header and footer in a single file

  • Next, create a file called footer.php. This file will have all of the footer HTML code.

<table width=80% height=30>

<tr><td>� <div align=center> UC Riverside Department<BR>� <a href=mailto:someuser@ucr.edu>someuser@ucr.edu</a>

</div>�</td></tr></table>

</body>

</html>

5 of 35

Examples

  • Step 3: Universal header and footer in a single file

  • This is the basic template that you will use on all of the pages. Make sure you name the files with a .php extension so that the server will process the PHP code. In this example, we assume the header and footer files are located in the same directory.

<?php

// header

include(“header.php”);

?>

Insert content here!

<?php

// footer

include(“footer.php”);

?>

6 of 35

Examples

Benefits:

- Any changes to header or footer only require editing of a

single file. This reduces the amount of work necessary for

site maintenance and redesign.

- Helps separate the content and design for easier maintenance

Page 1

Content

Page 5

Content

Page 3

Content

Page 2

Content

Page 4

Content

Header

Footer

7 of 35

Commons built in PHP function

  • phpinfo()
  • date()
  • include()
    • require()
    • require_once()
  • header()
  • isset()
  • printf()
  • exit()

8 of 35

phpinfo()

  • Print out php information
    • Installation
    • Version
    • Modules
    • Configuration
      • Variables

9 of 35

date()

  • returns a string of text for a certain date and time according to format you specify.
    • date (format,[timestamp]);
    • Timestamp is optional argument representing the number of seconds. If not specify, php will just use the current time on the server
  • Example:

  • http://my.php.net/manual/en/function.date.php

echo date(‘F j,Y’);

echo date(‘D’);

10 of 35

date() formatting

Character

Meaning

Example

Y

Years as 4 digits

2003

y

Years as 2 digits

03

n

Month as 1 or 2 digits

2

m

Month as 2 digits

02

F

Month

February

M

Month as 3 letters

Feb

j

Day of the month as 1 or 2 digits

8

d

Day of the month as 2 digits

08

l

Day of the week

Monday

D

Days of the week as 3 letters

Mon

g

Hour, 12 hour format as 1 or 2 digit

6

G

Hour, 24 hour format as 1 or 2 digit

18

H

Hour, 24 hour format as 1 or 2 digit

18

h

Hour, 12 hour format as 1 or 2 digit

06

11 of 35

include()

  • The include() statement includes and evaluates the specified file.
  • Concept: External files
    • Which allows you to divide your scripts into 2 parts
    • To extract HTML from your PHP or to separate out commonly used processes.
  • 4 functions for using external files
    • include(), include_once(), require() and require_once()
  • Example

include_once(“filename.php”);

require(‘/path/to/filename.html’);

require_one(‘filename.inc’)

12 of 35

Example using include()

header.inc

Content.php

footer.inc

include (‘./footer.inc’)

include (‘./header.inc’)

{

Code start

Html or php

}

13 of 35

include() vs require()

  • include() – PHP delays the actual loading of the requested file until the script reaches the point of executing the include statement
  • require() – executes the file regardless of whether it would have executed in the normal progression of the script

14 of 35

header()

  • header() function is used to redirect the web browser from current page to another.

header (“Location: http://www.url.com/page.php”);

  • The header function must be called before anything is sent to the web browser.

15 of 35

printf()

  • Output a formatted string

16 of 35

isset()

  • Determine whether a variable is set
  • If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL.
  • If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set.

17 of 35

Example isset()

18 of 35

exit()

  • Output a message and terminate the current script

19 of 35

Constant value

  • Use to defines a named constant
  • Example: WEB_LOCALHOST is a constant for localhost. This constant will have localhost value throughout the whole process.

20 of 35

Data types validation

  • is_numeric()
  • is_bool()
  • is_string()
  • is_int()
  • is_float()
  • is_object()
  • is_array()

21 of 35

is_numeric()

  • Check whether a variable is a number or numeric string
  • Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. (+0123.45e6 )
  • Example

22 of 35

is_bool()

  • Finds out whether a variable is a boolean
  • Returns TRUE if var is a boolean, FALSE otherwise.

23 of 35

is_string()

  • Find whether the type of a variable is string

is string

bool(true)

bool(true)

bool(false)

bool(false)

24 of 35

is_int()

  • Find whether the type of a variable is integer
  • To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().

25 of 35

is_array()

  • Finds whether a variable is an array
  • Returns TRUE if var is an array, FALSE otherwise.

26 of 35

Generating encoded passwords

  • Encode – to encode passwords into a unformatted string
  • Decode – to return to original data from encoded string

27 of 35

Encode

  • base64_encode() – return data encoded
  • Syntax

base64_encode ( string $data )

  • Example

VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==

28 of 35

Decode

  • base64_decode() – return the original data or false or failure
  • Syntax

base64_decode ( string $data)

  • Example

This is an encoded string

29 of 35

String Manipulation

  • str_word_count()
  • substr_count()
  • strlen()
  • md5()
  • strtolower()

30 of 35

str_word_count()

  • Return information about words used in a string
    • Counts the number of words inside string .
  • Syntax

str_word_count ( string $string )

  • Example

31 of 35

substr_count()

  • Count the number of substring occurrences
  • Syntax

int substr_count ( string $haystack , string $needle [, int $offset [, int $length ]] )

haystack – the string to search in

needle - the substring to search for

offset - the offset where to start counting

length - the maximum length after the specified offset to search for the substring.

32 of 35

Example substr_count()

33 of 35

strlen()

  • Get string length
  • Syntax

int strlen ( string $string )

  • Example

34 of 35

strtolower()

  • Make a string lowercase
  • Syntax

string strtolower ( string $str )

  • Example

35 of 35

Chapter 4: Review

  • When can be we used include()?
  • What is the purpose of header()?
  • strtolower() vs strtoupper)() ?
  • How to terminate a process or a page?