Sie sind auf Seite 1von 27

PHP: Introduction

Lecture No 6
T i Covered
Topics C d
z Server side web programming
z Client/Server systems
z Comparison with static HTML
z PHP - what is it? What does it do?
z PHP Language basics
z Syntax
z Variables,, Constants,, Operators
p
z Decision making
z PHP and the client
Cli t/S
Client/Server on the
th WWW
z Standard web sites operate on a
request/response basis
z A user requests t a resource E.g.
E HTML
document
z S
Server respondsd bby d
delivering
li i th the d
documentt
to the client
z Th client
The li t processes ththe d
documentt andd
displays it to user
S
Server Sid
Side Programming
P i
z Provides web site developers to utilise resources on
the web server
z Non-public
Non public resources do not require direct access
from the clients
z Allows web sites to be client agnostic (unless
JavaScript is used also)
z Most server side programming script is embedded
within
ithi markup
k ((although
lth hd
does nott h
have tto b
be,
sometimes better not to)
PHP - What
Wh t is
i it / does
d it do?
d ?
z PHP: PHP Hypertext Pre-processor
Pre processor
z Programming language that is interpreted
and executed on the server
z Execution is done before delivering content to
the client
z Contains a vast library of functionality that
programmers
p g can harness
z Executes entirely on the server, requiring no
specific features from the client
PHP - What
Wh t is
i it / does
d it do?
d ?
z Static resources such as regular HTML are simply output to
the client from the server
z Dynamic resources such as PHP scripts are processed on the
server prior to being output to the client
z PHP has the capability of connecting to many database
systems making the entire process transparent to the client

Web Page Request Load PHP File

PHP Engine –
Run Script
HTML Response PHP Results
User Web Server
PHP S
Summary
z PHP: PHP Hypertext Pre-processor
Pre processor
z Interpreted and executed by the server on
page requestt
z Returns simple output to the client
z Provides a tremendous amount of
functionality to programmers
z Can connect transparently to many database
systems
PHP L
Language Basics
B i
z Look at the building blocks of the PHP
language
z Syntax and structure
z Variables, constants and operators
z Data types and conversions
z Decision making IF and switch
z Interacting with the client application (HTML
forms)
PHP - Syntax
S t andd Structure
St t
z PHP is similar to C
z All scripts start with <?php and with with ?>
z Line separator:
p ; (semi-colon)
( )
z Code block: { //code here } (brace brackets)
z White space is generally ignored (not in strings)
z Comments are created using:
z // single line quote
z //* Multiple line block quote *//
z Precedence
z Enforced using parentheses
z E.g. $sum = 5 + 3 * 6; // would equal 23
z $sum = (5 + 3) * 6; // would equal 48
PHP - Variables
V i bl
z Prefixed with a $
z Assign values with = operator
z Example: $author
$ = “Trevor Adams”;
z No need to define type
z Variable names are case sensitive
z $author and $Author are different
PHP - Example
E l Script
S i t
z <?php
z $author = “Trevor Adams”;
z $msg = “Hello
Hello world!”;
world! ;
z echo $author . “ says ” . $msg;
z ?>
PHP - Constants
C t t
z Constants are special variables that cannot
be changed
z U th
Use them ffor named
d ititems th
thatt will
ill nott
change
z C t d using
Created i ad define
fi ffunction
ti
z define(‘milestokm’, 1.6);
z U d without
Used ith t $
z $km = 5 * milestokm;
PHP - Operators
O t
z Standard mathematical operators
z +, -, *, / and % (modulus)
z String concatenation with a period ((.))
z $car = “SEAT” . “ Altea”;
z echo $
$car;; would output
p “SEAT Altea”
z Basic Boolean comparison with “==”
z Using
g only
y = will overwrite a variable value
z Less than < and greater than >
z <= and >= as above but include equality
PHP - Data
D t TTypes
z PHP is not strictly typed
z Different to JAVA where all variables are declared
z A data type is either text or numeric
z PHP decides what type a variable is
z PHP can use variables in an appropriate way automatically
z Eg
E.g.
z $vat_rate = 0.175; /* VAT Rate is numeric */
z echo $vat_rate * 100 . “%”; //outputs “17.5%”
z $vat_rate is converted to a string for the purpose of the
echo statement
z Object,
j , Array y and unknown also exist as types,
yp , Be
aware of them but we shall not explore them today
PHP - embedded
b dd d language
l
z PHP can be placed directly inside HTML E
E.g.
g
z <html>
z <head><title>Basic
h d i l B i PHP page</title></head>
/il /h d
z <body>
z <h1><?php
h1 ? h echo h “H
“Hello
ll WWorld!;
ld! ??></h1>
/h1
z </body>
z </html>
/ht l
D i i
Decision Making
M ki - Basics
B i
z Decision making involves evaluating Boolean
expressions (true / false)
z If($catishungry) { /*
/ feed cat *// }
z “true” and “false” are reserved words
z Initialise as $valid = false;
z Compare with ==
z AND and OR for combinations
z E.g. if($catishungry AND $havefood) {/* feed
cat*/}}
PHP - IF statement
t t t
z Used to perform a conditional branch
z If (Boolean expression) {
z // one or more commands
d if true
z } else {
z // one or more commands if false
z }
PHP - Switch
S it h Statements
St t t
z Useful when a Boolean expression may have
many options E.g.
z switch($choice)
it h($ h i ) {
z case 0: { /* do things if choice equal 0 */ }
z C
Case 1
1: {/* ddo thi
things if choice
h i equall 1 */ }
z Case 2: {/* do things if choice equal 2 */ }
z Default: {/* do if choice is none of the above */}
z }
PHP - Dealing
D li with
ith the
th Client
Cli t
z All very nice but …
z … How is it useful in your web site?
z PHP allows you to use HTML forms
z Forms require technology at the server to
process them
z PHP is a feasible and good choice for the
processing of HTML forms
PHP - Dealing
D li with
ith the
th client
li t
z Quick re
re-cap
cap on forms
z Implemented with a <form> element in HTML
z Contains other input, text area, list controls
and options
z Has some method of submitting
PHP - Dealing
D li with
ith the
th client
li t
z Text fields
z Checkbox
z Radio button
z List boxes
z Hidden form fields
z Password box
z Submit and reset buttons
PHP - Dealing
D li with
ith the
th client
li t
z <form method=
method=“post”
post action=
action=“file
file.php
php” name=
name=“frmid”
frmid >
z Method specifies how the data will be sent
z Action specifies the file to go to. E.g. file.php
z id gives the form a unique name
z Post method sends all contents of a form with
basically hidden headers (not easily visible to users)
z Get method sends all form input in the URL requested
using name=value pairs separated by ampersands
(&)
z E.g. process.php?name=trevor&number=345
z Is visible in the URL shown in the browser
PHP - Dealing
D li with
ith the
th client
li t
z All form values are placed into an array
z Assume a form contains one textbox called
“txtName”
txtName and the form is submitted using the post
method, invoking process.php
z process.php could access the form data using:
z $_POST[‘txtName’]
z If the form used the get method, the form data would
b available
be il bl as:
z $_GET[‘txtName’]
PHP - Dealing
D li with
ith the
th client
li t
z For example,
example an HTML form:
z <form id=“showmsg” action=“show.php”
method= post >
method=“post”>
z <input type=“text” id=“txtMsg” value=“Hello World” />
z <input
p type=“submit”
yp id=“submit” value=“Submit”>
z </form>
PHP - Dealing
D li with
ith the
th client
li t
z A file called show
show.php
php would receive the
submitted data
z It could output the message
message, for example:
z <html>
z <head><title>Show Message</title></head>
z <body>
z <h1><?php echo $_POST[“txtMsg”]; ?></h1>
z </body>
z </html>
PHP IIntroduction
t d ti - Summary
S
z Topics covered
z Server side architecture brief overview
z Basic PHP language topics
z Syntax
z Variables Constants and Operators
Variables,
z Decision making, IF and Switch statements
z Dealing with the client
Useful Links and Further
St d
Study
z W3 Schools - http://www.w3schools.com/php/
http://www w3schools com/php/
z PHP web site - http://www.php.net/
z Choi W
Choi, W. (2000) Beginning PHP4
PHP4, Wrox Press
Press,
ISBN: 1-861003-73-0
z http://www.fcet.staffs.ac.uk/tja1/
p j
z Web site will be updated before accompanying tutorial
session
z Will contain
t i a useful
f l supplement
l t tto ttutorial
t i l content
t t

Das könnte Ihnen auch gefallen