Server-Side Web Development With PHP
Server-Side Web Development With PHP
PHP
Origins
Overview of PHP
copy (HTML)
interpret (PHP)
When the PHP processor finds markup code (which may include embedded client-side script) in
the input file, it simply copies it to the output file
When the processor encounters PHP script in the input file, it interprets it and sends any output
of the script to the output file.
This implies that the output from a PHP script must be HTML or XHTML, either of which could
include embedded client-side script.
General
Syntactic
PHP code can be specified in an HTML document internally or
externally:
Characteristics
Internally:
<?php
...
?>
General Syntactic
Characteristics
Variables
Expressions
Strings
Strings
10
Strings
Boolean
13
Arithmetic functions
14
strtolower, strtoupper
15
16
Implicit (coercions)
String to numeric and vice versa
17
Explicit conversions
1.
2.
1.
1.
2.
Variable type
two different
ways
determination
19
Assignment Operators
20
Output
21
Output
<html>
<head><title> Trivial php example </title>
</head>
<body>
<?php
print "<b>Welcome to my home page <br /> <br />";
print "Today is:</b> ";
print date("l, F jS");
print "<br />";
?>
</body>
</html>
22
Date function
Control Statements
Control Expressions
The usual six (>, <, >=,<=, !=, and ==) have
the usual meanings.
PHP also has ===, which produces TRUE only
if both operands are the same type and have
the same value, and !==, the opposite of ===.
If the types of the operands of the other six
relational operators are not the same, one is
coerced to the type of the other
24
Boolean operators
same as C
There are six Boolean operators: and,
or, xor, !, &&, and ||.
The and and && operators perform the
same operation, as do or and ||.
The difference between them is that the
precedence of and and or is lower than
that of && and ||.
25
Selection statements
if statement
26
as in C
The switch expression type must be
integer, double, or string
27
Loop Statements
28
do-while Statement
29
For Statement
30
Example
Example
<?php
$a = 7;
$b = 7;
if ($a == $b) {
$a = 3 * $a;
?>
<br /> At this point, $a and $b are
equal <br />
So, we change $a to three times $a
<?php
}
?>
33
Arrays
34
Array creation
There are two ways to create an array in PHP
1. Assigning a value to a subscripted variable that previously was not an array
creates the array
if no array named $listcurrently exists, the following statement creates one:
$list[0] = 17;
If empty brackets are used in an assignment to an array, a numeric
subscript is implicitly furnished
The furnished subscript is 1 greater than the largest used so far in the
array if the array already has elements with numeric keys
$list[1] = Today is my birthday!;$list[] = 42;
35
Array creation
2.
$list = array(1 => 17, 2 => 24, 3 => 42, 4 => 91);
36
Array creation
37
Use brackets
$list[4] = 7;
$list["day"] = "Tuesday";
$list[] = 17;
39
40
41
42
43
$color = current($colors);
print("$color <br />");
while ($color = next($colors))
print ("$color <br />");
44
45
46
47
48
Examples:
$ages = array("Bob" => 42, "Mary" => 43);
foreach ($ages as $name => $age)
print("$name is $age years old <br />");
49
50
51
SHOW sorting.php
52
User-Defined Functions
Syntactic form:
function function_name(formal_parameters) {
}
General Characteristics
functions definition does not need to appear in a
document before the function is called placement is
irrelevant
function overloading is not allowed
Functions can have a variable number of parameters
Default parameter values are supported
Function definitions can be nested
Function names are NOT case sensitive
The return function is used to return a value;
If there is no return, there is no returned value
53
Parameters
55
56
57
58
59
Pattern Matching
60
Pattern Matching
The preg_split function operates on strings but
returns an array and uses patterns
The function takes two parameters, the first of which is
a Perl-style pattern as a string.
The second parameter is the string to be split. For
example, consider the following sample code:
$fruit_string = apple : orange : banana;
$fruits = preg_split(/ : /, $fruit_string);
The array $fruits now has (apple, orange,
banana).
The following example illustrates the use of preg_split
on text to parse out the words and produce a
frequency-of-occurrence table:
SHOW word_table.php
61
Form Handling
62
Cookies
Cookies
In PHP, cookies are created with setcookie
setcookie(cookie_name, cookie_value, lifetime)
64
Session Tracking
Session Tracking
66