0% found this document useful (0 votes)
38 views

Nicko Gamba: Slide 1

This document provides an overview of PHP (Hypertext Preprocessor) in 19 slides. It discusses PHP as a server-side scripting language used for web development, comparing it to static HTML. Key topics covered include PHP syntax and structure, variables, constants, operators, data types, decision making using if and switch statements, and embedding PHP code within HTML. The document provides examples to illustrate PHP concepts and explains how PHP scripts are executed on the server before delivering content to the client.

Uploaded by

nicko gamba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Nicko Gamba: Slide 1

This document provides an overview of PHP (Hypertext Preprocessor) in 19 slides. It discusses PHP as a server-side scripting language used for web development, comparing it to static HTML. Key topics covered include PHP syntax and structure, variables, constants, operators, data types, decision making using if and switch statements, and embedding PHP code within HTML. The document provides examples to illustrate PHP concepts and explains how PHP scripts are executed on the server before delivering content to the client.

Uploaded by

nicko gamba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Slide 1

Hyp ertext Pre- p roc essor

PHP
NICKO GAMBA
Slide 2

• Server Sid e Web Prog ra m m ing


• Client/Server System s
• Com p a rison w ith Sta tic HTML
TOPICS • PHP—Wha t is it? Wha t d oes it d o?
COVERED
• PHP La ng ua g e Ba sic s
• Synta x
• Va ria b les, Consta nts, Op era tors
• Dec ision Ma king

• PHP & the Client


Slide 3

• Sta nd a rd w eb site op era tes on a


CLIENT req uest/resp onse b a sis

SERVER • A user req uests a resourc e. E.g .


HTML d oc um ent
ON THE
• Server resp ond s b y d elivering
WWW the d oc um ent to the c lient

World Wid e Web • The c lient p roc esses the


d oc um ent a nd d isp la ys it to the
user
Slide 4

• Provid es w eb site d evelop ers to


utilize resourc es on the w eb
server
SERVER SIDE • Non- p ub lic resourc es d o not
req uire d irec t a c c ess from the
PROGRAMMING c lients
• Allow s w eb sites to b e c lient
a g nostic (unless Ja va Sc rip t is
used a lso)
• Most server sid e p rog ra m m ing
sc rip t is em b ed d ed w ithin
m a rkup (a lthoug h d oes not ha ve
to b e, som etim es b etter not to)
Slide 5

• PHP: PHP Hyp ertext Pre- p roc essor


• Prog ra m m ing la ng ua g e tha t is
interp reted a nd exec uted on the
PHP—WHAT IS server
• Exec ution is d one b efore
IT/DOES IT DO? d elivering c ontent to the c lient
• Conta ins va st lib ra ry of
func tiona lity tha t p rog ra m m ers
c a n ha rness
• Exec utes entirely on the server,
req uiring no sp ec ific fea tures
from the c lient
• Develop ed b y Ra sm us Lerd orf
Slide 6

• Sta tic resourc es suc h a s HTML


a re sim p ly outp ut to the c lient
from the server
PHP—WHAT IS • Dyna m ic resourc es suc h a s PHP
IT/DOES IT DO? sc rip ts a re p roc essed on the
server p rior to b eing outp ut to
the c lient

• PHP ha s the c a p a b ility of


c onnec ting to m a ny d a ta b a se
system s m a king the entire
p roc ess tra nsp a rent to the c lient
Slide 7

Web
page
req uest
User HTML

PHP—WHAT IS resp onse

IT/DOES IT DO?
Web Server

PHP Loa d
results PHP file

PHP Eng ine—run


sc rip t
Slide 8

• PHP: PHP Hyp ertext Pre- p roc essor


• Interp reted a nd exec uted b y the
server on the p a g e req uest
PHP • Returns sim p le outp ut to the

SUMMARY
c lient
• Provid es a trem end ous a m ount
of func tiona lity to the
p rog ra m m ers
• Ca n c onnec t tra nsp a rently to
m a ny d a ta b a se system s
Slide 9

Look a t the b uild ing b loc ks of the


PHP — PHP la ng ua g e
• Synta x a nd struc ture
LANGUAGE • Va ria b les, c onsta nts, a nd

BASICS
op era tors
• Da ta typ es a nd c onversions
• Dec ision m a king if a nd switch
• Intera c ting w ith the c lient
a p p lic a tion (HTML form s)
Slide 10

• PHP is sim ila r to C


• All sc rip ts sta rt w ith <?php a nd
PHP — end s w ith ?>
• Line sep a ra tor: ; (sem ic olon)
SYNTAX & • Cod e b loc k: { (c od e here) }

STRUCTURE • White sp a c e is g enera lly ig nored


(not in string s)
• Prec ed enc e
• Enforc ed using p a rentheses
• E.g . $sum = 5 + 3 * 6; // 23
• $sum = (5 + 3) * 6; // 48
• Com m ents a re c rea ted using
• // sing le line q uote
• /* m ultip le line b loc k q uote */
Slide 11

• Prefixed w ith a $
• Assig n va lues w ith = op era tor:
Ex: $author = “Trevor Adams”;
PHP — • No need to d efine typ e

VARIABLES • Va ria b le na m es a re ca se
sensitive:
$author a nd $Author a re
d ifferent
Slide 12

PHP —
EXAMPLE
SCRIPT
Slide 13

• Consta nts a re sp ec ia l va ria b les


tha t c a nnot b e c ha ng ed . Use
them for na m ed item s tha t w ill
PHP — not c ha ng e
• Crea ted using a d efine func tion
CONSTANTS •

define(“BIRTHDAY”, “January 1, 1990”);
Used w ithout $
• $birthday = “My birthday is ” .
BIRTHDAY
Slide 14

• Sta nd a rd m a them a tic a l


op era tors
• +, - , *, / a nd %(m od ulus)

PHP — • String c onc a tena tion w ith a .


(p eriod )
OPERATORS • Ba sic Boolea n c om p a rison w ith
“==”
• Using only w ill = overw rite a va ria b le va lue
• Less tha n < a nd g rea ter tha n >
• <= a nd >= a s a b ove b ut inc lud e eq ua lity
Slide 15

• PHP is not stric tly typ ed


• Different to Ja va w here a ll va ria b les
a re d ec la red
• A d a ta typ e is either a text or
PHP — num eric . E.g .:
• $vat_rate = 0.175; /* VAT Rate

DATA TYPES is numeric */


• echo $vat_rate * 100 . “%”;;
//outputs “17.5%”
• $vat_rate is c onverted to a string
for the p urp ose of the echo
sta tem ent
• Ob jec t, Arra y, a nd unknow n a lso
exist a s typ es
Slide 16

PHP c a n b e p la c ed d irec tly insid e


HTML

PHP — <html>

EMBEDDED <head><title>Basic PHP


page</title></head>

LANGUAGE
<body>
<h1><?php echo “Hello World!; ?></h1>
</body>
</html>
Slide 17

• Dec ision m a king involves


DECISION eva lua ting Boolea n exp ression
(true/false)
MAKING - • if($catIsHungry){ /*feed cat*/
}
BASICS • “true” a nd “fa lse” a re reserved
w ord s
• Initia lize a s $valid = false;
• Com p a re w ith ==
• AND a nd OR for c om b ina tions
• E.g . if($catIsHungry AND
$haveFood){ /*feed cat*/ }
Slide 18

• Used to p erform c ond itiona l


b ra nc h
if(Boolean expression){
PHP — // one or more commands if true

IF STATEMENTS
} else {
// one or more commands if
false
}
Slide 19

• Useful w hen a Boolea n


exp ression m a y ha ve m a ny
op tions. E.g .
PHP — switch($choice) {
SWITCH case 0:

STATEMENTS
//code if $choice === 0
break;
case 1:
//code if $choice === 1
break;
default: // if none was true
}
Slide 20

First few step s a re c ruc ia l—top ic s


PHP—ONE c overed :
• Ba sic struc ture a nd synta x
SMALL STEP • Va ria b les, c onsta nts, a nd op era tors
• Da ta typ es a nd c onversions
FOR A MAN • Dec ision m a king
Any q uestions so fa r?
Slide 21

All very nic e b ut…how is it useful in


PHP—DEALING your w eb site?
• PHP a llow s you to use HTML form s
WITH THE • Form s req uire tec hnolog y a t the
CLIENT server to p roc ess them
• PHP is a fea sib le a nd g ood
c hoic e for the p roc essing of
HTML form s
Slide 22

• Text field s
PHP—DEALING • Chec kb ox
• Ra d io b utton
WITH THE • List b oxes
CLIENT • Hid d en form field s
• Pa ssw ord b ox
• Sub m it a nd reset b uttons
Slide 23

PHP—DEALING
WITH THE
CLIENT
Slide 24

• <form method=“post”
PHP—DEALING action=“process.php” name=“data”>
• Method sp ec ifies how d a ta w ill b e sent

WITH THE •

Ac tion sp ec ifies the file to g o to
Id g ives the form a uniq ue na m e

CLIENT • Post m ethod send s a ll c ontents of a


form w ith b a sic a lly hid d en hea d ers
(not ea sily visib le to the users)
• Get m ethod send s a ll form inp ut in the
URL req uested using na m e=va lue p a irs
sep a ra ted b y a m p ersa nd s (&)
• E.g . process.php?name=nicko&age=205
is visib le in the URL show n in the b row ser
Slide 25

• All form va lues a re p la c ed into a n


PHP—DEALING a rra y.
Assum e a form c onta ins one

WITH THE
textb ox c a lled txtName a nd the
form is sub m itted using the post
m ethod , invoking process.php.
CLIENT Process.php c ould a c c ess the
form d a ta using :
$_POST[“txtName”];
If the form used the get m ethod ,
the form d a ta w ould b e a va ila b le
as
$_GET[“txtName”];
Slide 26

For exa m p le a n HTML form :


PHP—DEALING <form id=“showmsg” action=“show.php”
method=“post”>

WITH THE
<input type=“text” id=“txtMsg”
value=“Hello World”>
<input type=“submit” id=“submit”
CLIENT value=“Submit”>
</form>
Slide 27

• A file c a lled show .p hp w ould rec eive


PHP—DEALING the sub m itted d a ta . It c ould outp ut the
m essa g e, for exa m p le:

WITH THE <html>


<head><title>Show
CLIENT Message</title></head>
<body>
<h1><?php echo
$_POST[“txtMsg”]; ?></h1>
</body>
</html>
Slide 28

• Form elem ents c onta in inp ut elem ents


PHP—DEALING • Ea c h inp ut elem ent ha s a n id

WITH THE • In the form is p osted , the file sta ted a s


the a c tion c a n use:

CLIENT • $_GET[“inputId”];
• If a form uses the get m ethod

SUMMARY • $_POST[“inputId”];
• Ensure you set a ll ID a ttrib utes for form
elem ents a nd their c ontents
Note for slide 24th

PHP does not use the ID attribute to extract user input from the textbox—PHP ignores it.
PHP uses the name attribute to select the textbox and extract its input.

Example:

<input type=”text” name=”txtInput” placeholder=”Enter any text”>

<?php

$txtInput = $_POST[“txtInput”];

?>

You might also like