to get the file
to get the file
to get the file
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Lecture 6.<br />
PHP: Part 1<br />
1
Origins and Uses of PHP<br />
• Developed by Rasmus Lerdorf, a member of <strong>the</strong> Apache<br />
Group, in 1994 <strong>to</strong> track visi<strong>to</strong>rs <strong>to</strong> his Web site<br />
• PHP‟s development was transferred <strong>to</strong> a small group of<br />
devoted volunteers and now is an open-source product<br />
• PHP is originally an acronym for Personal Home Page, later<br />
PHP: Hypertext Preprocessor<br />
• PHP, as a server-side scripting language, is used for form<br />
handling, <strong>file</strong> processing, and database access<br />
• has driver support for 15 database systems<br />
• supports electronic mail pro<strong>to</strong>cols POP3 and IMAP<br />
• supports distributed object architectures COM and CORBA<br />
2
Overview of PHP<br />
• PHP is a server-side scripting language whose scripts are<br />
embedded in HTML documents<br />
• Similar <strong>to</strong> JavaScript, but on <strong>the</strong> server side<br />
• PHP is an alternative <strong>to</strong> CGI, Active Server Pages (ASP),<br />
and Java Server Pages (JSP)<br />
• Filename extension is .php, .php3, or .phtml<br />
• PHP syntax is similar <strong>to</strong> that of JavaScript<br />
• PHP is dynamically typed<br />
• PHP has an extensive library of functions, making a flexible<br />
and powerful<br />
• http://www.php.net<br />
3
Overview of PHP<br />
• The PHP processor has two modes: copy (XHTML) and<br />
interpret (PHP).<br />
• PHP processor takes a PHP document as input and<br />
produces an XHTML document <strong>file</strong><br />
• When it finds XHTML code in <strong>the</strong> input <strong>file</strong>, simply<br />
copies it <strong>to</strong> <strong>the</strong> output <strong>file</strong><br />
• When it finds PHP script, it interprets it and send any<br />
output of <strong>the</strong> script <strong>to</strong> <strong>the</strong> output <strong>file</strong><br />
• This new output <strong>file</strong> is sent <strong>to</strong> <strong>the</strong> requesting browser.<br />
• The client never sees <strong>the</strong> PHP script.<br />
4
Operation Overview<br />
Web browser<br />
Client<br />
Request<br />
a page<br />
Transfer<br />
XHTML <strong>file</strong><br />
Request<br />
data<br />
processing<br />
Web Server<br />
Generate<br />
XHMTL <strong>file</strong><br />
O<strong>the</strong>r<br />
Server<br />
Platform<br />
PHP Engine<br />
DB<br />
5
General Syntactic Characteristics<br />
• PHP code can be specified in an HTML document<br />
internally or externally:<br />
Internally:<br />
<br />
Externally: include ("myScript.inc")<br />
• The included <strong>file</strong> can contain XHTML or client-side<br />
script as well as PHP<br />
• If <strong>the</strong> <strong>file</strong> has PHP, <strong>the</strong> PHP must be in ,<br />
even if <strong>the</strong> include appears in <br />
6
General Syntactic Characteristics<br />
• All variable names begin with $<br />
• The name part is like <strong>the</strong> names of variables in many<br />
programming languages: a letter or an underscore<br />
followed by any number of letters, digits, or underscores<br />
• PHP variables are case sensitive, but reserved words<br />
and function names are not. (Table 12.1)<br />
(Ex) while, WHILE, While, and wHiLe are same<br />
• Comments - three different kinds (Java and Perl)<br />
(a) // ... ; for single line<br />
(b) # ... ; for single line<br />
(c) /* ... */ ; for multiple-line<br />
7
PHP Reserved Words<br />
and else function not this<br />
break elseif global or true<br />
case extends if require var<br />
class false include return virtual<br />
continue for list static xor<br />
default foreach new switch while<br />
do<br />
8
General Syntactic Characteristics<br />
• PHP statements are terminated with semicolons.<br />
• Compound statements for control structures are<br />
formed with braces<br />
• Unless used as <strong>the</strong> body of a function definition,<br />
compound statements cannot be blocks<br />
• They cannot define locally scoped variables<br />
9
Example<br />
10
PHP TYPES<br />
• Four scalar types<br />
• Boolean<br />
• Integer<br />
• Double<br />
• String<br />
• Two compound types<br />
• Array<br />
• Object *<br />
• Two special types<br />
• NULL<br />
• Resource *<br />
* will not be covered here.<br />
11
Variables<br />
• There are no type declarations because of dynamic typing<br />
• The type of a variable is set every time it is assigned a value<br />
• An unassigned ( unbound ) variable has <strong>the</strong> value, NULL<br />
• A variable of NULL in <strong>the</strong> expression will be coerced <strong>to</strong> a value<br />
dictated by <strong>the</strong> context of <strong>the</strong> use<br />
(Ex) If a number, NULL is coerced <strong>to</strong> 0<br />
If a string, NULL is coerced <strong>to</strong> <strong>the</strong> empty string<br />
• The unset function sets a variable <strong>to</strong> NULL<br />
• The IsSet function tests whe<strong>the</strong>r a variable is NULL<br />
(Ex) IsSet ( $fruit ) returns TRUE if $fruit has non-NULL value<br />
12
Variables<br />
• If you prefer <strong>to</strong> be informed when an unbound variable<br />
is referenced, place <strong>the</strong> error_reporting(15) at <strong>the</strong><br />
beginning of <strong>the</strong> script in <strong>the</strong> document <strong>file</strong><br />
• The error_reporting function is used <strong>to</strong> change <strong>the</strong> error –<br />
reporting level of <strong>the</strong> PHP interpreter<br />
• The default error-reporting level is 7<br />
• PHP has many predefined variables, including <strong>the</strong><br />
environment variables of <strong>the</strong> host operating system<br />
• You can <strong>get</strong> a list of <strong>the</strong> predefined variables by<br />
calling phpinfo() in a script<br />
13
primitive types<br />
• Integer type corresponds <strong>to</strong> <strong>the</strong> long type of C and its<br />
successors<br />
• Double type corresponds <strong>to</strong> <strong>the</strong> double type of C and<br />
its successors. Decimal point, an exponent, or both.<br />
• There is no character type<br />
• A single character data value is a string of length one<br />
• Strings<br />
• Characters are single bytes<br />
• String literals use single (′)or double quotes(″)<br />
14
Example<br />
• Integer<br />
$var = 1234;<br />
$var = -1234;<br />
$var = 01234;<br />
$var = 0x1234;<br />
// Decimal<br />
// Decimal<br />
// Octal<br />
// Hexadecimal<br />
• Floating point<br />
$a = 1.234; // Float<br />
$a = 0.1234e1; // Exponential<br />
15
String<br />
• Single-quoted string literals<br />
• Embedded variables are NOT interpolated<br />
(Ex) „The sum is: $sum‟ = The sum is: $sum<br />
• Embedded escape sequences are NOT recognized<br />
• Double-quoted string literals<br />
• Embedded variables ARE interpolated<br />
(Ex) “The sum is: $sum” = The sum is: 12.4<br />
• If <strong>the</strong>re is a variable name in a double-quoted string but<br />
you don‟t want it interpolated, it must be backslashed<br />
• Embedded escape sequences ARE recognized<br />
• For both single- and double-quoted literal strings,<br />
embedded delimiters must be backslashed<br />
16
Special character<br />
Special char.<br />
\n<br />
\r<br />
\t<br />
\\<br />
\$<br />
\”<br />
Semantics<br />
new line<br />
Return carriage<br />
Tab<br />
Backslash<br />
Dollar<br />
Quotation mark<br />
17
Boolean<br />
• The only two possible values are TRUE and FALSE<br />
(case insensitive)<br />
• If an integer expression is used in Boolean context, it<br />
is FALSE if it is zero; o<strong>the</strong>rwise, it is TRUE.<br />
• If a string expression is used in Boolean context, it is<br />
FALSE if it is ei<strong>the</strong>r <strong>the</strong> empty string or <strong>the</strong> string "0";<br />
o<strong>the</strong>rwise, it is TRUE.<br />
18
Arithmetic Opera<strong>to</strong>rs and Expressions<br />
• +, -, *, /, %, ++, --<br />
• For +,- and *, if ei<strong>the</strong>r operand is double, <strong>the</strong>n it<br />
produces a double result<br />
• If <strong>the</strong> result of integer division is not an integer, a<br />
double is returned<br />
• Any integer operation that results in overflow produces<br />
a double<br />
• The modulus opera<strong>to</strong>r (%) coerces its non-integer<br />
operands <strong>to</strong> integer<br />
19
Predefined Functions<br />
Functi<br />
on<br />
Paramet<br />
er<br />
Returns<br />
floor Double Largest integer less than or equal <strong>to</strong> <strong>the</strong> parameter<br />
ceil<br />
Double<br />
round Double Nearest integer<br />
srand Integer<br />
rand<br />
abs<br />
min<br />
Two<br />
numbers<br />
Integer or<br />
double<br />
Numbers Smallest<br />
Smallest integer greater than or equal <strong>to</strong> <strong>the</strong><br />
parameter<br />
Initializes a random number genera<strong>to</strong>r with <strong>the</strong><br />
parameter<br />
A pseudorandom number greater than <strong>the</strong> first<br />
parameter and smaller than <strong>the</strong> second<br />
Absolute value of <strong>the</strong> parameter<br />
max<br />
Numbers Largest<br />
20
String Operations and Functions<br />
• The only opera<strong>to</strong>r is period(.) for catenation<br />
• String variables can be treated like arrays for access <strong>to</strong><br />
individual characters<br />
• The position can be specified in braces<br />
(Ex) If $str has “apples”, $str{3} is <strong>the</strong> fourth (l)<br />
• Functions:<br />
• strlen, strcmp, strpos, substr, as in C<br />
• chop – remove whitespace from <strong>the</strong> right end<br />
• trim – remove whitespace from both ends<br />
• ltrim – remove whitespace from <strong>the</strong> left end<br />
• str<strong>to</strong>lower, str<strong>to</strong>upper<br />
21
String functions<br />
Function Parameter Returns<br />
strlen A string The no. of characters in <strong>the</strong> string<br />
strcmp Two strings 0, negative, or positive number<br />
strpos Two strings The character position...<br />
substr<br />
A string and<br />
an integer<br />
The substring of <strong>the</strong> string, starting from<br />
<strong>the</strong> second parameter (possibly, third<br />
parameter for length)<br />
chop A string Remove all whitespace from its end<br />
trim A string Remove all whitespace from both ends<br />
ltrim A string Remove all whitespace from its begin<br />
str<strong>to</strong>lower A string All uppercase letters converted <strong>to</strong><br />
lowercase<br />
str<strong>to</strong>upper A string All lowercase letters converted <strong>to</strong><br />
uppercase<br />
22
Example<br />
$str = “Apples are good”;<br />
$sub = substr ($str, 7, 1);<br />
The value of $sub is „a‟.<br />
23
Scalar Type Conversions<br />
• Implicit type conversions (coercions)<br />
• The context of an expression determines <strong>the</strong> type that is<br />
expected or required<br />
• When a numeric value appears in string context, <strong>the</strong><br />
numeric value is coerced <strong>to</strong> a string<br />
• When a string value appears in numeric context, <strong>the</strong><br />
string value is coerced <strong>to</strong> a number.<br />
• String <strong>to</strong> numeric<br />
• If <strong>the</strong> string contains an e or an E, it is converted <strong>to</strong><br />
double; o<strong>the</strong>rwise <strong>to</strong> integer<br />
• If <strong>the</strong> string does not begin with a sign or a digit, <strong>the</strong><br />
conversion fails and zero is used<br />
• Nonnumeric characters following <strong>the</strong> number in <strong>the</strong><br />
string are ignored.<br />
24
Scalar Type Conversions<br />
• Explicit type conversions in three different ways<br />
• Using cast: (type_name) exp<br />
(Ex) $<strong>to</strong>tal = 4.333; (int) $<strong>to</strong>tal // 4<br />
• Using function: intval, doubleval, strval<br />
(Ex) $<strong>to</strong>tal = 4.333; intval($<strong>to</strong>tal) // 4<br />
• Using settype function: settype (variable, type_name)<br />
(Ex) settype($<strong>to</strong>tal, "integer") // 4<br />
• The type of a variable can be determined with <strong>get</strong>type<br />
or type testing functions<br />
• <strong>get</strong>type($<strong>to</strong>tal) - returns <strong>the</strong> type of <strong>the</strong> current value.<br />
It may return "unknown"<br />
• is_integer($<strong>to</strong>tal), is_float(), is_bool(), is_string()<br />
– a predicate function<br />
25
Output<br />
• Any output from a PHP script becomes part of <strong>the</strong><br />
document <strong>the</strong> PHP processor is building<br />
• An output from a PHP script is in <strong>the</strong> form of XHTML<br />
that is sent <strong>to</strong> <strong>the</strong> browser<br />
• XHTML is sent <strong>to</strong> <strong>the</strong> browser through standard output<br />
• There are three ways <strong>to</strong> produce output :<br />
• echo<br />
• print<br />
• printf<br />
26
Output<br />
• echo function<br />
• If paren<strong>the</strong>ses are included, only a single string parameter<br />
is acceptable<br />
• O<strong>the</strong>rwise, any number of parameters are acceptable<br />
(Ex) echo "whatever", "it may ";<br />
echo ("first ") ;<br />
• returns no value<br />
• echo and print take a string, but coerce o<strong>the</strong>r values<br />
<strong>to</strong> strings<br />
(Ex) echo 47 or print(47) will produce 47<br />
27
Output<br />
• print function:<br />
• Called with only one parameter, possibly in a paren<strong>the</strong>sis<br />
(Ex) print "Welcome <strong>to</strong> my site!";<br />
• Will coerce non-string type value <strong>to</strong> a string<br />
• Return a value (1 if successful, 0 o<strong>the</strong>rwise)<br />
• printf function<br />
• Is like its counterpart in C<br />
• Can control <strong>the</strong> format of displayed data completely<br />
(Ex) $day = “Tuesday”;<br />
$high = 79;<br />
printf (“The high on %7s was %3d”, $day, $high);<br />
28
Example<br />
<br />
<br />
<strong>to</strong>day.php <br />
<br />
<br />
<br />
<br />
<br />
<br />
Welcome <strong>to</strong> my home page<br />
<br />
Today is: Saturday, June 1st<br />
29
Control Statements<br />
• Relational Opera<strong>to</strong>rs<br />
• Use eight relational opera<strong>to</strong>rs of JavaScript<br />
• > , < , >= ,
Control Statements<br />
• Selection statements<br />
• if, if-else, elseif<br />
• switch - as in C<br />
• while - just like C<br />
• do-while - just like C<br />
• for - just like C<br />
• foreach - discussed later<br />
• break - in any for, foreach, while, do-while, or switch<br />
• continue - in any loop<br />
31
Alternative compound delimiters<br />
• Applicable <strong>to</strong> if, switch, for, and while control<br />
statements<br />
• Opening delimiter is <strong>the</strong> colon with its own closing<br />
reserved word<br />
• More readable.<br />
while ($a < 100) {<br />
$a = $a * $b + 7;<br />
$b++;<br />
}<br />
while ($a < 100) :<br />
$a = $a * $b + 7;<br />
$b++;<br />
endwhile;<br />
SHOW powers.html<br />
32
Example<br />
if ($day ==== “Saturday” || $day == “Sunday”)<br />
$<strong>to</strong>day = “weekend”;<br />
else {<br />
$<strong>to</strong>day = “weekday”;<br />
$work = true;<br />
}<br />
switch ($bordersize) {<br />
case “0”: print “”; break;<br />
case “1”: print “”; break;<br />
case “4”: print “”; break;<br />
case “8”: print “”; break;<br />
default: print “Error-invalid value: $bordersize ”;<br />
}<br />
33
Example<br />
$fact = 1;<br />
$count = 1;<br />
while ($count < $n) {<br />
$count ++;<br />
$fact *= $count;<br />
}<br />
for ($count = 1, $fact = 1; $count < $n) {<br />
$count++;<br />
$fact *= $count;<br />
}<br />
34
HTML/PHP document<br />
• HTML can be intermingled with PHP script<br />
35
Example<br />
<br />
<br />
powers.php <br />
<br />
<br />
<br />
Powers table <br />
<br />
Number <br />
Square Root <br />
Square <br />
Cube <br />
Quad <br />
<br />
36
Result<br />
Powers table<br />
Number Square Root Square Cube Quad<br />
1 1 1 1 1<br />
2 1.4142125623731 4 8 16<br />
3 1.7320508075689 9 27 81<br />
4 2 16 64 256<br />
5<br />
6<br />
7<br />
8<br />
9<br />
10<br />
38
PHP in HTML<br />
• HTML<br />
<br />
<br />
PHP <br />
Programming <br />
<br />
<br />
• PHP in HTML<br />
<br />
<br />
<br />
<br />
<br />
39
PHP in HTML<br />
• Using variable<br />
<br />
<br />
<br />
<br />
<br />
<br />
40