Chapter 4: PHP scripting- part 2
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
Objectives
Multiple PHP files
<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> |
Examples
<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> |
Examples
<?php // header include(“header.php”); ?> Insert content here! <?php // footer include(“footer.php”); ?> |
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
Commons built in PHP function
phpinfo()
date()
echo date(‘F j,Y’);
echo date(‘D’);
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 |
include()
include_once(“filename.php”);
require(‘/path/to/filename.html’);
require_one(‘filename.inc’)
Example using include()
header.inc
Content.php
footer.inc
include (‘./footer.inc’)
include (‘./header.inc’)
{
Code start
Html or php
}
include() vs require()
header()
header (“Location: http://www.url.com/page.php”);
printf()
isset()
Example isset()
exit()
Constant value
Data types validation
is_numeric()
is_bool()
is_string()
is string
bool(true)
bool(true)
bool(false)
bool(false)
is_int()
is_array()
Generating encoded passwords
Encode
base64_encode ( string $data )
VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
Decode
base64_decode ( string $data)
This is an encoded string
String Manipulation
str_word_count()
str_word_count ( string $string )
substr_count()
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.
Example substr_count()
strlen()
int strlen ( string $string )
strtolower()
string strtolower ( string $str )
Chapter 4: Review