Lect6 PHP1 PDF
Lect6 PHP1 PDF
PHP: Part 1
1
Origins and Uses of PHP
Developed by Rasmus Lerdorf, a member of the Apache
Group, in 1994 to track visitors to his Web site
2
Overview of PHP
PHP is a server-side scripting language whose scripts are
embedded in HTML documents
Similar to JavaScript, but on the server side
PHP is an alternative to CGI, Active Server Pages (ASP),
and Java Server Pages (JSP)
Filename extension is .php, .php3, or .phtml
PHP syntax is similar to that of JavaScript
PHP is dynamically typed
PHP has an extensive library of functions, making a flexible
and powerful
http://www.php.net
3
Overview of PHP
The PHP processor has two modes: copy (XHTML) and
interpret (PHP).
PHP processor takes a PHP document as input and
produces an XHTML document file
When it finds XHTML code in the input file, simply
copies it to the output file
When it finds PHP script, it interprets it and send any
output of the script to the output file
This new output file is sent to the requesting browser.
The client never sees the PHP script.
4
Operation Overview
Request
a page
Other
Server PHP Engine DB
Platform
5
General Syntactic Characteristics
PHP code can be specified in an HTML document
internally or externally:
Internally: <?php
...
?>
6
General Syntactic Characteristics
All variable names begin with $
The name part is like the names of variables in many
programming languages: a letter or an underscore
followed by any number of letters, digits, or underscores
PHP variables are case sensitive, but reserved words
and function names are not. (Table 12.1)
(Ex) while, WHILE, While, and wHiLe are same
Comments - three different kinds (Java and Perl)
(a) // ... ; for single line
(b) # ... ; for single line
(c) /* ... */ ; for multiple-line
7
PHP Reserved Words
do
8
General Syntactic Characteristics
PHP statements are terminated with semicolons.
Compound statements for control structures are
formed with braces
Unless used as the body of a function definition,
compound statements cannot be blocks
They cannot define locally scoped variables
9
Example
10
PHP TYPES
12
Variables
If you prefer to be informed when an unbound variable
is referenced, place the error_reporting(15) at the
beginning of the script in the document file
13
primitive types
Integer type corresponds to the long type of C and its
successors
Strings
Characters are single bytes
String literals use single (′)or double quotes(″)
14
Example
Integer
Floating point
$a = 1.234; // Float
$a = 0.1234e1; // Exponential
15
String
Single-quoted string literals
Embedded variables are NOT interpolated
(Ex) „The sum is: $sum‟ = The sum is: $sum
Embedded escape sequences are NOT recognized
17
Boolean
18
Arithmetic Operators and Expressions
+, -, *, /, %, ++, --
19
Predefined Functions
Functi Paramet
Returns
on er
floor Double Largest integer less than or equal to the parameter
21
String functions
23
Scalar Type Conversions
Implicit type conversions (coercions)
The context of an expression determines the type that is
expected or required
When a numeric value appears in string context, the
numeric value is coerced to a string
When a string value appears in numeric context, the
string value is coerced to a number.
String to numeric
If the string contains an e or an E, it is converted to
double; otherwise to integer
If the string does not begin with a sign or a digit, the
conversion fails and zero is used
Nonnumeric characters following the number in the
string are ignored.
24
Scalar Type Conversions
Explicit type conversions in three different ways
Using cast: (type_name) exp
(Ex) $total = 4.333; (int) $total // 4
Using function: intval, doubleval, strval
(Ex) $total = 4.333; intval($total) // 4
Using settype function: settype (variable, type_name)
(Ex) settype($total, "integer") // 4
26
Output
echo function
If parentheses are included, only a single string parameter
is acceptable
Otherwise, any number of parameters are acceptable
(Ex) echo "whatever", "it may <br />";
echo ("first <br />") ;
returns no value
echo and print take a string, but coerce other values
to strings
(Ex) echo 47 or print(47) will produce 47
27
Output
print function:
Called with only one parameter, possibly in a parenthesis
(Ex) print "Welcome to my site!";
Will coerce non-string type value to a string
Return a value (1 if successful, 0 otherwise)
printf function
Is like its counterpart in C
Can control the format of displayed data completely
(Ex) $day = “Tuesday”;
$high = 79;
printf (“The high on %7s was %3d”, $day, $high);
28
Example
<!-- today.php: An example to illustrate a php document -->
<html>
<head> <title> today.php </title>
</head>
<body>
<p>
<?php
print “<b>Welcome to my home page <br /><br />”;
print “Today is: “;
print date(“l, F jS”);
print “<br />”;
?>
</p>
</body>
Welcome to my home page
</html>
Today is: Saturday, June 1st
29
Control Statements
Relational Operators
Use eight relational operators of JavaScript
> , < , >= , <= , != , == , === , !==
For string op number
If the string converted to a number, do numeric
comparison
If the string not converted to a number, do string
comparison
For string op string
If both converted to numbers, do numeric comparison
Boolean operators
(and , &&), (or , ||) , xor , !
The precedence of and and or is higher than that of &&
and || 30
Control Statements
Selection statements
if, if-else, elseif
switch - as in C
while - just like C
do-while - just like C
for - just like C
foreach - discussed later
break - in any for, foreach, while, do-while, or switch
continue - in any loop
31
Alternative compound delimiters
Applicable to if, switch, for, and while control
statements
Opening delimiter is the colon with its own closing
reserved word
More readable.
SHOW powers.html
32
Example
if ($day ==== “Saturday” || $day == “Sunday”)
$today = “weekend”;
else {
$today = “weekday”;
$work = true;
}
switch ($bordersize) {
case “0”: print “<table>”; break;
case “1”: print “<table border = „1‟>”; break;
case “4”: print “<table border = „4‟>”; break;
case “8”: print “<table border = „8‟>”; break;
default: print “Error-invalid value: $bordersize <br />”;
}
33
Example
$fact = 1;
$count = 1;
while ($count < $n) {
$count ++;
$fact *= $count;
}
34
HTML/PHP document
35
Example
<!-- powers.php
An example to illustrate loops and arithmetic -->
<html>
<head><title> powers.php </title>
</head>
<body>
<table border = "border">
<caption> Powers table </caption>
<tr>
<th> Number </th>
<th> Square Root </th>
<th> Square </th>
<th> Cube </th>
<th> Quad </th>
</tr>
36
<?php
for ($number = 1; $number <=10; $number++) {
$root = sqrt($number);
$square = pow($number, 2);
$cube = pow($number, 3);
$quad = pow($number, 4);
print("<tr align = 'center'> <td> $number </td>");
print("<td> $root </td> <td> $square </td>");
print("<td> $cube </td> <td> $quad </td> </tr>");
}
?>
</table>
</body>
</html>
37
Result
Powers table
Number Square Root Square Cube Quad
1 1 1 1 1
2 1.4142125623731 4 8 16
3 1.7320508075689 9 27 81
4 2 16 64 256
5
6
7
8
9
10
38
PHP in HTML
HTML
<html>
<body>
<h1> PHP </h1><br>
<h2> Programming </h2>
</body>
</html>
PHP in HTML
<html>
<body>
<?php
print(“<h1> PHP </h1><br>”);
print(“<h2> Programming </h2>”);
?>
</body>
</html>
39
PHP in HTML
Using variable
<?php
$time = date(“Y - m - d - H : i : s ”);
?>
<html>
<body>
<?php
// Print current time...
print(“<h2> Current time is $time. </h2>”);
?>
</body>
</html>
40