E Content PHP
E Content PHP
dha,
Lect
urer GPPanchkul
a
Chapt
er1
Dev
elopi
ngPor
tal
sUsi
ngHTML
Whati
sHTML?
HTMLst
andsf
orHy
perTex
tMar
kupLanguage.I
tisusedt
odesi
gnwebpages
usi
ngmar
kupl
anguage.HTMLi
sthecombi
nat
ionofHy
per
textandMar
kupl
anguage.
Hy
per
textdef
inest
hel
i
nkbet
weent
hewebpages.Mar
kupl
anguagei
susedt
odef
ine
t
het
extdocumentwi
thi
ntagwhi
chdef
inest
hest
ruct
ureofwebpages.Thi
slanguagei
s
usedt
oannot
ate(
makenot
esf
ort
hecomput
er)t
extsot
hatamachi
necanunder
stand
i
tandmani
pul
atet
extaccor
dingl
y.Mostofmar
kup(
e.g.HTML)l
anguagesar
ehuman
r
eadabl
e.Languageusest
agst
odef
inewhatmani
pul
ati
onhast
obedoneont
het
ext
.
TheHi
stor
yofHTML
Hypert
extmeansat extthatcont ai
nsref
er ences(l
inks)t
o ot
hertextsthat
vi
ewerscanaccessi mmedi ately
.Hepubl i
shedthef ir
stversi
onofHTMLi n1991,
consi
sti
ngof18HTMLt ags.Sincethen,eachnewv ersi
onoftheHTMLlanguagecame
wit
hnewt agsandat
tri
butes( t
agmodi fi
ers)t
othemar kup.
Accor
dingtoMozill
aDev el
operNet
work’
sHTMLEl ementRef
erence,cur
rentl
y,
ther
eare140HTMLt ags,alt
houghsomeofthem ar
eal
readyobsol
ete(notsupport
ed
bymodernbrowser
s).
Duet o aquickri
sei npopular
it
y,HTMLi snow considered anoff
ici
alweb
st
andard.TheHTMLspeci f
icat
ionsaremaint
ainedanddev el
opedbyt heWorl
dWide
WebConsor t
ium (
W3C).Youcancheckoutthelatestst
ateofthelanguageanyt
imeon
W3C’swebsite.
Thebiggestupgr
adeoft hel
anguagewastheint
roduct
ionofHTML5in2014.I
t
addedsev er
alnew semantictagst
othemarkup,thatr
evealt
hemeaningoft
heirown
content,
suchas
<art
icl
e>,<header>,
and<footer
>.
HTMLVERSI
ON YEAR
HTML1.
0 1991
NehaMi
dha,
Lect
urer GPPanchkul
a
HTMLVERSI
ON YEAR
HTML2.
0 1995
HTML3.
2 1997
HTML4.
01 1999
XHTML 2000
HTML5 2014
I
ntr
oduct
iont
oHTML5
HTML5i
sthef
if
thandcur
rentv
ersi
onofHTML.I
thasi
mpr
ovedt
hemar
kup
av
ail
abl
efordocument
sandhasi
ntr
oducedappl
i
cat
ionpr
ogr
ammi
ngi
nter
faces(
API
)
andDocumentObj
ectModel
(DOM)
.
Feat
ures:
Ithasint
roducednewmul
ti
medi
afeat
ureswhi
chsuppor
tsaudi
oandv
ideo
cont
rol
sbyusi
ng<audi
o>and<v
ideo>t
ags.
Ther
ear
enewgr
aphi
csel
ement
sincl
udi
ngv
ect
orgr
aphi
csandt
ags.
Enr
ichsemant
iccont
entbyi
ncl
udi
ng<header
><f
oot
er>,
<ar
ti
cle>,
<sect
ion>and
<f
igur
e>ar
eadded.
Dr
agandDr
op-Theusercangr
abanobj
ectanddr
agi
tfur
therdr
oppi
ngi
t
onanewl
ocat
ion.
Geo-
locat
ionser
vices-I
thel
pst
olocat
ethegeogr
aphi
cal
locat
ionofacl
i
ent
.
Webst
oragef
aci
l
itywhi
chpr
ovi
deswebappl
i
cat
ionmet
hodst
ost
oredat
aonweb
br
owser
.
UsesSQLdat
abaset
ost
oredat
aof
fl
ine.
Al
l
owst
odr
awv
ari
ousshapesl
i
ket
ri
angl
e,r
ect
angl
e,ci
rcl
e,et
c.
Capabl
eofhandl
i
ngi
ncor
rectsy
ntax.
EasyDOCTYPEdecl
arat
ioni
.e.<!
doct
ypeht
ml>
Easychar
act
erencodi
ngi
.e.<met
achar
set
=”UTF-
8″>
NehaMi
dha,
Lect
urer GPPanchkul
a
Remov
edel
ement
sfr
om HTML5
Ther
ear
emanyel
ement
swhi
char
edepr
eci
atedf
rom HTML5ar
eli
sted
bel
ow:
REMOVEDELEMENTS USEI
NSTEADELEMENTS
<acr
ony
m> <abbr
>
<appl
et> <obj
ect
>
<basef
ont
> CSS
<bi
g> CSS
<cent
er> CSS
<di
r> <ul
>
<f
ont
> CSS
<f
rame>
<f
rameset
>
<nof
rames>
<i
sindex>
<st
ri
ke> CSS,
<s>or<del
>
<t
t> CSS
NewAddedEl
ement
sinHTML5
<ar
ti
cle>:The<ar
ti
cle>t
agi
susedt
orepr
esentanar
ti
cle.Mor
especi
fi
cal
l
y,t
he
cont
entwi
thi
nthe<ar
ti
cle>t
agi
sindependentf
rom t
heot
hercont
entoft
hesi
te
(
event
houghi
tcanber
elat
ed)
.
<asi
de>:The<asi
de>t
agi
susedt
odescr
ibet
hemai
nobj
ectoft
hewebpagei
na
shor
terwayl
i
keahi
ghl
i
ght
er.I
tbasi
cal
l
yident
if
iest
hecont
entt
hati
srel
atedt
othe
pr
imar
y
NehaMi
dha,
Lect
urer GPPanchkul
a
cont
entoft
hewebpagebutdoesnotconst
it
utet
hemai
nint
entoft
hepr
imar
ypage.The
<asi
de>t
agcont
ainsmai
nlyaut
hori
nfor
mat
ion,
li
nks,
rel
atedcont
entandsoon.
<f
igcapt
ion>:The<f
igur
ecapt
ion>t
agi
nHTMLi
susedt
osetacapt
iont
othef
igur
e
el
ementi
nadocument
.
<f
igur
e>:The<f
igur
e>t
agi
nHTMLi
susedt
oaddsel
f-
cont
ainedcont
entl
i
ke
i
l
lust
rat
ions,di
agr
ams,phot
osorcodesl
i
sti
ngi
nadocument
.Iti
srel
atedt
omai
n
f
lowbuti
tcanbeusedi
nanyposi
ti
onofadocumentandt
hef
igur
egoeswi
tht
he
f
low oft
hedocumentandi
fremov
eitt
heni
tshoul
dnotaf
fectt
hef
low oft
he
document
.
<header
>:I
tcont
ainst
hesect
ionheadi
ngaswel
lasot
hercont
ent
,suchasa
nav
igat
ionl
i
nks,
tabl
eofcont
ent
s,et
c.
<f
oot
er>:The<f
oot
er>t
agi
nHTMLi
susedt
odef
ineaf
oot
erofHTMLdocument
.
Thi
s sect
ion cont
ains t
he f
oot
eri
nfor
mat
ion (
aut
hori
nfor
mat
ion,copy
right
i
nfor
mat
ion,
car
ri
erset
c).Thef
oot
ert
agar
eusedwi
thi
nbodyt
ag.The<f
oot
er>t
ag
i
snewi
ntheHTML5.Thef
oot
erel
ement
srequi
reast
artt
agaswel
lasanendt
ag.
<mai
n>:Del
i
neat
est
hemai
ncont
entoft
hebodyofadocumentorwebapp.
<mar
k>:The<mar
k>t
agi
nHTMLi
susedt
odef
inet
hemar
kedt
ext
.Iti
susedt
o
hi
ghl
i
ghtt
hepar
toft
het
exti
nthepar
agr
aph.
<nav
>:The <nav
>tag i
sused t
o decl
ari
ng t
he nav
igat
ionalsect
ion i
n HTML
document
s.Websi
test
ypi
cal
l
yhav
esect
ionsdedi
cat
edt
onav
igat
ionall
i
nks,
whi
ch
enabl
esusert
onav
igat
ethesi
te.Thesel
i
nkscanbepl
acedi
nsi
deanavt
ag.
<sect
ion>:I
tdemar
cat
esat
hemat
icgr
oupi
ngofcont
ent
.
<det
ail
s>:The<det
ail
s>t
agi
susedf
ort
hecont
ent
/i
nfor
mat
ionwhi
chi
sini
ti
all
y
hi
ddenbutcoul
dbedi
spl
ayedi
ftheuserwi
shest
oseei
t.Thi
stagi
susedt
ocr
eat
e
i
nter
act
ivewi
dgetwhi
chusercanopenorcl
osei
t.Thecont
entofdet
ail
stagi
s
v
isi
blewhenopent
hesetat
tri
but
es.
<summar
y>:The<summar
y>t
agi
nHTMLi
susedt
odef
ineasummar
yfort
he
<det
ail
s> el
ement
.The <summar
y> el
ementi
s used al
ong wi
tht
he <det
ail
s>
el
ementandpr
ovi
desasummar
yvi
sibl
etot
heuser
.Whent
hesummar
yiscl
i
cked
byt
heuser
,thecont
entpl
acedi
nsi
det
he<det
ail
s>el
ementbecomesv
isi
blewhi
ch
waspr
evi
ousl
yhi
dden.The
<summar
y>t
agwasaddedi
nHTMl5.The<summar
y>t
agr
equi
resbot
hst
art
ing
andendi
ngt
ag.
NehaMi
dha,
Lect
urer GPPanchkul
a
<t
ime>:The<t
ime>t
agi
susedt
odi
spl
ayt
hehuman-
readabl
edat
a/t
ime.I
tcanal
so
be used t
o encode dat
es and t
imes i
n a machi
ne-
readabl
efor
m.The mai
n
adv
ant
agef
oruser
sist
hatt
heycanof
fert
oaddbi
rt
hdayr
emi
nder
sorschedul
ed
ev
ent
sint
hei
rcal
ender
’sandsear
chengi
nescanpr
oducesmar
tersear
chr
esul
ts.
<bdi
>:The<bdi
>tagr
efer
stot
heBi
-Di
rect
ionalI
sol
ati
on.I
tdi
ff
erent
iat
eat
extf
rom
ot
hert
extt
hatmaybef
ormat
tedi
ndi
ff
erentdi
rect
ion.Thi
stagi
susedwhenauser
gener
atedt
extwi
thanunknowndi
rect
ions.
<wbr
>:The<wbr
>tagi
nHTMLst
andsf
orwor
dbr
eakoppor
tuni
tyandi
susedt
o
def
inet
heposi
ti
onwi
thi
nthet
extwhi
chi
str
eat
edasal
i
nebr
eakbyt
hebr
owser
.It
i
smost
lyusedwhent
heusedwor
dist
ool
ongandt
her
ear
echancest
hatt
he
br
owsermaybr
eakl
i
nesatt
hewr
ongpl
acef
orf
it
ti
ngt
het
ext
.
<dat
ali
st>:The<dat
ali
st>t
agi
susedt
opr
ovi
deaut
ocompl
etef
eat
urei
ntheHTML
f
il
es.I
tcanbeusedwi
thi
nputt
ag,
sot
hatuser
scaneasi
l
yfi
l
lthedat
aint
hef
orms
usi
ngsel
ectt
hedat
a.
<key
gen>:The<key
gen>t
agi
nHTMLi
susedt
ospeci
fyakey
-pai
rgener
atorf
iel
din
af
orm.The pur
pose of<key
gen> el
ementi
sto pr
ovi
de a secur
e way t
o
aut
hent
icat
euser
s.Whenaf
rom i
ssubmi
tt
edt
hent
wokey
sar
egener
ated,
pri
vat
e
keyandpubl
i
ckey
.Thepr
ivat
ekeyst
oredl
ocal
l
y,andt
hepubl
i
ckeyi
ssentt
othe
ser
ver
.Thepubl
i
ckeyi
susedt
ogener
atecl
i
entcer
ti
fi
cat
etoaut
hent
icat
euserf
or
f
utur
e.
<out
put
>:The<out
put
>tagi
nHTMLi
susedt
orepr
esentt
her
esul
tofacal
cul
ati
on
per
for
medbyt
hecl
i
ent
-si
descr
iptsuchasJav
aScr
ipt
.
<pr
ogr
ess>:I
tisusedt
orepr
esentt
hepr
ogr
essofat
ask.I
tisal
sodef
inet
hathow
muchwor
kisdoneandhow muchi
slef
ttodownl
oadat
hings.I
tisnotusedt
o
r
epr
esentt
hedi
skspaceorr
elev
antquer
y.
<sv
g>:I
tist
heScal
abl
eVect
orGr
aphi
cs.
<canv
as>:The<canv
as>t
agi
nHTMLi
susedt
odr
awgr
aphi
csonwebpageusi
ng
Jav
aScr
ipt
.Itcanbeusedt
odr
awpat
hs,
boxes,
text
s,gr
adi
entandaddi
ngi
mages.
Bydef
aul
titdoesnotcont
ainsbor
derandt
ext
.
<audi
o>:I
tdef
inest
hemusi
coraudi
ocont
ent
.
<embed>:Def
inescont
ainer
sforext
ernal
appl
i
cat
ions(
usual
l
yav
ideopl
ayer
).
<sour
ce>:I
tdef
inest
hesour
cesf
or<v
ideo>and<audi
o>.
NehaMi
dha,
Lect
urer GPPanchkul
a
<t
rack>:I
tdef
inest
het
racksf
or<v
ideo>and<audi
o>.
<vi
deo>:I
tdef
inest
hev
ideocont
ent
.
Adv
ant
ages
Al
lbr
owser
ssuppor
ted.
Mor
edev
icef
ri
endl
y.
Easyt
ouseandi
mpl
ement
.
HTML5i
nint
egr
ati
onwi
thCSS,
Jav
aScr
ipt
,et
ccanhel
pbui
l
dbeaut
if
ulwebsi
tes.
Di
sadv
ant
ages:
Longcodeshav
etobewr
it
tenwhi
chi
sti
meconsumi
ng.
Onl
ymoder
nbr
owser
ssuppor
tit
.
Di
ff
erencebet
weenHTMLandHTML5
HTMLst
andsf
orHy
perTex
tMar
kupLanguage.I
tisusedt
odesi
gnwebpages
usi
ngamar
kupl
anguage.HTMLi
sthecombi
nat
ionofHy
per
textandMar
kupl
anguage.
Hy
per
textdef
inest
hel
i
nkbet
weent
hewebpages.Amar
kupl
anguagei
susedt
odef
ine
t
het
extdocumentwi
thi
ntagwhi
chdef
inest
hest
ruct
ureofwebpages.Thi
slanguagei
s
usedt
oannot
ate(
att
henot
eforcomput
er)t
extsot
hatamachi
necanunder
standi
t
andmani
pul
atet
extaccor
dingl
y.Mostoft
hemar
kup(
e.g.HTML)l
anguagesar
ehuman
r
eadabl
e.Thel
anguageusest
agst
odef
inewhatmani
pul
ati
onhast
obedoneont
he
t
ext
.Iti
susedf
orst
ruct
uri
ngandpr
esent
ingt
hecont
entont
hewebpages.HTML5i
s
t
hef
if
thv
ersi
onofHTML.Manyel
ement
sar
eremov
edormodi
fi
edf
rom HTML5.
Ther
ear
emanydi
ff
erencesbet
weenHTMLandHTML5whi
char
edi
scussedbel
ow:
HTML HTML5
I
tdidn’
tsupportaudi
oandv ideowit
houtt
he Itsuppor
tsaudi
oandv ideocontrol
swith
the
useoffl
ashplayersupport
.
useof<audio>and<video>tags.
ItusesSQLdatabasesandapplicat
ion
cacheto
I
tusescookiestostoretemporar
ydata.
storeoff
li
nedata.
NehaMi
dha,
Lect
urer GPPanchkul
a
All
owsJav
aScr
iptt
oruni
nbackgr
ound.
This
i
spossi
bleduet
oJSWebwor
kerAPIi
n
Doesnotall
owJavaScr
ipttoruni
nbr
owser
.
HTML5.
Vect
orgraphi
csi
spossibleinHTMLwit
h
t
hehel
pofv
ari
oust
echnol
ogi
essuchas Vect
orgr
aphi
csi
saddi
ti
onal
l
yani
ntegr
al
VML, apar
tofHTML5l
i
keSVGandcanv
as.
Si
lv
er-
li
ght
,Fl
ash,
et c.
I
tdoesnotal
l
owdr aganddr
opef
fect
s. I
tal
l
owsdr
aganddr
opef
fect
s.
Notpossi
blet
odr
awshapesl
i
keci
rcl
e, HTML5al
l
owst
odr
awshapesl
i
keci
rcl
e,
r
ect
angl
e,t
ri
angl
eet
c. r
ect
angl
e,t
ri
angl
eet
c.
I
tsuppor
tedbyal
lnewbr
owserl
i
keFi
ref
ox,
I
twor
kswi
thal
lol
dbr
owser
s. Mozi
l
la,
Chr
ome,
Saf
ari
,et
c.
Ol
derv
ersi
onofHTMLar
elessmobi
l
e-
f
ri
endl
y. HTML5l
anguagei
smor
emobi
l
e-f
ri
endl
y.
Doct
ypedecl
arat
ioni
stool
ongand
compl
i
cat
ed. Doct
ypedecl
arat
ioni
squi
tesimpl
eand
easy
.
Newelementf
orwebstr
uctur
eli
kenav
,
El
ement
sli
kenav
,headerwer
enotpr
esent
.header
,foot
eret
c.
Characterencodi
ngislongand Char
act
erencodi
ngi
ssi
mpl
eandeasy
.
complicated.
Iti
salmosti mpossi
bletogett
rue Onecant
rackt
heGeoLocat
ionofauser
GeoLocation easi
l
y
ofuserwi
ththehel
pofbrowser. byusi
ngJSGeoLocati
onAPI.
I
tcannothandlei
naccur
atesynt
ax. I
tiscapabl
eofhandl
i
nginaccur
atesy
ntax.
Att
ribut
esli
kechar
set
,asy
ncandpi
ngar
e Att
ri
but
esofchar
set
,asy
ncandpi
ngar
ea
par
t
absentinHTML.
ofHTML5.
NehaMi
dha,
Lect
urer GPPanchkul
a
Whati
sCSS
Cascadi
ngSt
yleSheet
s,f
ondl
yref
err
edt
oasCSS,i
sasi
mpl
edesi
gnl
anguage
i
ntendedt
osi
mpl
i
fyt
hepr
ocessofmaki
ngwebpagespr
esent
abl
e.
CSShandl
est
hel
ookandf
eel
par
tofawebpage.Usi
ngCSS,
youcancont
rol
the
col
oroft
het
ext
,thest
yleoff
ont
s,t
hespaci
ngbet
weenpar
agr
aphs,
howcol
umnsar
e
si
zedandl
aidout
,whatbackgr
oundi
magesorcol
orsar
eused,l
ayoutdesi
gns,and
v
ari
ati
onsi
ndi
spl
ayf
ordi
ff
erentdev
icesandscr
eensi
zesaswel
lasav
ari
etyofot
her
ef
fect
s.
CSSi
seasyt
olear
nandunder
standbuti
tpr
ovi
despower
fulcont
rolov
ert
he
pr
esent
ati
on ofan HTMLdocument
.Mostcommonl
y,CSS i
scombi
ned wi
tht
he
mar
kupl
anguagesHTMLorXHTML.
Adv
ant
agesofCSS
CSSsav
est
ime−Youcanwr
it
eCSSonceandt
henr
eusesamesheeti
nmul
ti
ple
HTMLpages.Youcandef
ineast
ylef
oreachHTMLel
ementandappl
yitt
oas
manyWebpagesasy
ouwant
.
Pagesl
oadf
ast
er−I
fyouar
eusi
ngCSS,y
oudonotneedt
owr
it
eHTMLt
ag
at
tri
but
esev
eryt
ime.Justwr
it
eoneCSSr
uleofat
agandappl
yitt
oal
lthe
occur
rencesoft
hatt
ag.Sol
esscodemeansf
ast
erdownl
oadt
imes.
Easymai
ntenance−Tomakeagl
obalchange,si
mpl
ychanget
hest
yle,andal
l
el
ement
sinal
lthewebpageswi
l
lbeupdat
edaut
omat
ical
l
y.
Super
iorst
ylest
oHTML−CSShasamuchwi
derar
rayofat
tri
but
est
hanHTML,
soy
oucangi
veaf
arbet
terl
ookt
oyourHTMLpagei
ncompar
isont
oHTML
at
tri
but
es.
Mul
ti
pleDev
iceCompat
ibi
l
ity−St
ylesheet
sal
l
ow cont
entt
obeopt
imi
zedf
or
mor
ethanonet
ypeofdev
ice.Byusi
ngt
hesameHTMLdocument
,di
ff
erent
v
ersi
onsofawebsi
tecanbepr
esent
edf
orhandhel
ddev
icessuchasPDAsand
cel
lphonesorf
orpr
int
ing.
NehaMi
dha,
Lect
urer GPPanchkul
a
Gl
obalwebst
andar
ds−Now HTMLat
tri
but
esar
ebei
ngdepr
ecat
edandi
tis
bei
ngr
ecommendedt
ouseCSS.Soi
tsagoodi
deat
ost
artusi
ngCSSi
nal
lthe
HTMLpagest
omaket
hem compat
ibl
etof
utur
ebr
owser
s.
Whati
sCSS3
CSS3i
sdesi
gnedpr
imar
il
ytoenabl
ethesepar
ati
onofdocumentcont
entf
rom
documentpr
esent
ati
on,i
ncl
udi
ngaspect
ssuchast
hel
ayout
,col
ors,andf
ont
s.I
tcan
al
sobeusedt
odi
spl
ayt
hewebpagedi
ff
erent
lywhi
chcanchangedependi
ngony
our
scr
eensi
ze.Changest
othedesi
gnofadocumentcanbeappl
i
edqui
ckl
yandeasi
l
y.
Someoft
hemosti
mpor
tantCSS3modul
esar
e:
Sel
ect
ors
BoxModel
Backgr
oundsandBor
der
s
I
mageVal
uesandRepl
acedCont
ent
TextEf
fect
s
2D/
3DTr
ansf
ormat
ions
Ani
mat
ions
Mul
ti
pleCol
umnLay
out
UserI
nter
face
Basi
cst
ruct
ureofHTML
The<HTML>i
samar
kupl
anguagewhi
chi
susedbyt
hebr
owsert
omani
pul
ate
t
ext
,imagesandot
hercont
entt
odi
spl
ayi
tinr
equi
redf
ormat
.
Tagsi
nHTML
Tagsar
eoneoft
hemosti
mpor
tantpar
tinanHTMLDocument
.HTMLuses
somepr
edef
inedt
agswhi
cht
ell
sthebr
owseraboutcont
entdi
spl
aypr
oper
ty,t
hati
s
howt
odi
spl
ayapar
ti
cul
argi
vencont
ent
.ForExampl
e,t
ocr
eat
eapar
agr
aph,
onemust
uset
hepar
agr
aph t
ags(
<p></
p>)and t
oinser
tan i
mageonemustuset
hei
mg
t
ags(
<img/
>).
NehaMi
dha,
Lect
urer GPPanchkul
a
Ther
ear
egener
all
ytwot
ypesoft
agsi
nHTML:
Pai
redTagsorCl
osedTag:Theset
agscomei
npai
rs.Thati
stheyhav
ebot
hopeni
ng(
<>)
andcl
osi
ng(
</>)t
ags.
Si
ngul
arTagsOpenTag:orEmpt
yTag:
Theset
agsdonotr
equi
redt
obecl
osed.Bel
owi
san
exampl
eof(
<b>)t
agi
nHTML,
whi
cht
ell
sthebr
owsert
obol
dthet
exti
nsi
dei
t.
AnHTMLDocumenti
smai
nlydi
vi
dedi
ntot
wopar
ts:
HEAD:
Thi
scont
ainst
hei
nfor
mat
ionaboutt
heHTMLdocument
.ForExampl
e,
Headt
agcont
ainsmet
adat
a,t
it
le,
pageCSSet
c.Al
ltheHTMLel
ement
sthatcan
beusedi
nsi
det
he
<head>el
ementar
e:
<sty
le>
<ti
tl
e>
<base>
<noscri
pt>
<scri
pt>
<meta>
BODY:
Thi
scont
ainsev
ery
thi
ngy
ouwantt
odi
spl
ayont
heWebPage.Themost
commonel
ement
stobepl
acedi
ntheHTML<body
>tagar
e:<h1>,
<p>,
<di
v>,
<t
abl
e>t
agset
c.
Basi
cHTMLDocument
I
nit
ssi
mpl
estf
orm,
fol
l
owi
ngi
sanexampl
eofanHTMLdocument
:
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>Thi
si
sdocumentt
it
le</
tit
le>
</
head>
<body
>
<h1>Thi
si
saheadi
ng</
h1>
<p>Documentcont
entgoesher
e .</
p>
</
body
>
</
html
>
NehaMi
dha,
Lect
urer GPPanchkul
a
Ei
theryoucanuseTr yitopti
onavail
abl
eatthetopri
ghtcor
nerofthecodebox
t
ocheckt heresultofthi
sHTMLcode,orl et'
ssaveiti
nanHTMLf i
l
et est
.ht
m using
yourf
avori
tetexteditor
.Finall
yopeni tusi
ngawebbr owserli
keInt
ernetExpl
oreror
Googl
eChr ome,orFir
efoxetc.Itmustshowthefol
l
owingout
put:
The<!
DOCTYPE>Decl
arat
ion
The<!DOCTYPE>declarati
ontagisusedbyt hewebbr owsert
ounder
standthe
ver
sionoft
heHTMLusedi nthedocument
.Cur
rentver
sionofHTMLis5anditmakes
useofthef
oll
owingdecl
arati
on:
<!
DOCTYPEht
ml>
Therearemanyot herdeclar
ati
ontypeswhichcanbeusedi
nHTMLdocument
dependi
ngonwhatv er
sionofHTMLi sbeingused.Wewil
lseemoredet
ail
sont
his
whil
ediscussi
ng
<!
DOCTYPE. .
.>tagal
ongwithotherHTMLtags.
Headi
ngTags
Anydocumentst ar
tswi t
haheading.Youcanusediff
erentsi
zesforyourheadi
ngs.
HTMLal sohassi xlevelsofheadings,whi
chusetheelements<h1>,<h2>,<h3>,
<h4>,<h5>,and<h6>.Whi l
edisplayi
nganyheadi
ng,browseraddsonel i
nebefor
e
andonelineaft
erthatheadi
ng.
Exampl
e:
<!DOCTYPEhtml>
<html>
<head>
<t i
tle>Headi
ngExampl
e</ti
tle>
</head>
<body>
<h1>Thisi sheadi
ng1</h1>
NehaMi
dha,
Lect
urer GPPanchkul
a
<h2>Thi
sisheadi
ng2</
h2>
<h3>Thi
sisheadi
ng3</
h3>
<h4>Thi
sisheadi
ng4</
h4>
<h5>Thi
sisheadi
ng5</
h5>
<h6>Thi
sisheadi
ng6</
h6>
</body>
</html>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t:
Par
agr
aphTag
The<p>tagoffer
sawaytost
ruct
ureyourtexti
ntodi
ffer
entpar
agraphs.Each
par
agr
aphoft
extshoul
dgoi
nbet
weenanopening<p>andaclosi
ng</p>tagasshown
bel
owint
heexample:
<!DOCTYPEht ml>
<html>
<head>
<t i
tle>ParagraphEx ample</ ti
tle>
</head>
<body>
<p>Herei saf irst paragraphof t ext.
</p>
<p>Herei sas econdpar agraphof text.
</ p>
<p>Herei sat hirdpar agraphof text.</
p>
</body>
</html>
Her
eisaf
ir
stpar
agr
aphof
t
ext
.Her
eisasecond
par
agr
aphoft
ext
.
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t:
NehaMi
dha,
Lect
urer GPPanchkul
a
Li
neBr
eakTag
Wheneveryouusethe<br/>el
ement,
anyt
hingfol
l
owingitst
art
sfr
om t
henextl
i
ne.
Thi
stagisanexampleofanempt yel
ement
,whereyoudonotneedopeni
ngand
cl
osi
ngtags,asther
eisnothi
ngtogoinbet
weenthem.
The<br/>taghasaspacebet weenthecharacter
sbrandtheforwardslash.I
f
y
ouomitt
hisspace,ol
derbrowserswil
lhavetr
oublerender
ingt
helinebreak,
whi l
eif
y
oumissthef
orwardslashcharact
erandjustuse<br>iti
snotval
idinXHTML.
Hor
izont
alLi
nes
Hori
zontal
li
nesareusedtovi
suall
ybreak-
upsect
ionsofadocument
.The<hr>
tagcreatesali
nefrom t
hecurr
entposi
ti
oninthedocumenttother
ightmar
ginand
breakstheli
neaccordi
ngly
.
Agai
n<hr/
>tagisanexampl
eoftheempt
yelement,wher
eyoudonotneed
openi
ngandcl
osi
ngtags,
asther
eisnot
hingt
ogoinbetweenthem.
The<hr/>elementhasaspacebet weenthecharacter
shrandthefor
ward
slash.I
fyouomitthisspace,ol
derbrowserswil
lhavet
roublerender
ingt
hehori
zont
al
l
ine,whil
eifyoumissthef or
wardslashcharact
erandjustuse<hr>i
tisnotv
ali
din
XHTML
HTMLAt
tri
but
es
Wehav
eseenf
ew HTMLt
agsandt
hei
rusagel
i
keheadi
ngt
ags<h1>,<h2>,
par
agr
apht
ag<p>andot
hert
ags.Weusedt
hem sof
ari
nthei
rsi
mpl
estf
orm,but
mostoft
heHTMLt
agscanal
sohav
eat
tri
but
es,
whi
char
eext
rabi
tsofi
nfor
mat
ion.
Anat
tri
but
eisusedt
odef
inet
hechar
act
eri
sti
csofanHTMLel
ementandi
s
pl
acedi
nsi
det
heel
ement
'sopeni
ngt
ag.Al
lat
tri
but
esar
emadeupoft
wopar
ts:
aname
andav
alue:
Thenamei
sthepr
oper
tyy
ouwantt
oset
.Forexampl
e,t
hepar
agr
aph<p>
el
ementi
ntheexampl
ecar
ri
esanat
tri
but
ewhosenamei
sal
i
gn,
whi
chy
oucan
uset
oindi
cat
etheal
i
gnmentofpar
agr
aphont
hepage.
Thev
aluei
swhaty
ouwantt
hev
alueoft
hepr
oper
tyt
obesetandal
way
sput
wi
thi
nquot
ati
ons.Thebel
owexampl
eshowst
hreepossi
blev
aluesofal
i
gn
at
tri
but
e:l
eft
,cent
erandr
ight
.
NehaMi
dha,
Lect
urer GPPanchkul
a
At
tri
but
enamesandat
tri
but
eval
uesar
ecase-
insensi
ti
ve.Howev
er,
theWor
ld
Wi
deWebConsor
ti
um (
W3C)r
ecommendsl
ower
caseat
tri
but
es/
att
ri
but
eval
ues
i
nthei
rHTML4r
ecommendat
ion.
Exampl
e
<!
DOCTYPEht ml>
<html>
<head>
<titl
e >Ali
gnAt t ribute Example</ ti tle>
</head>
<body>
<pa li
gn ="
left">Thi sisl eftal igned< /p>
<pa li
gn ="
center">Thisi scenter al igned</ p>
<pa li
gn ="
right">Thisi sr i
ght al igned</ p>
</body>
</html>
Thi
swi
l
ldi
spl
ayt
hef
oll
owi
ngr
esul
t:
Thi
sisl
eftal
igned
Thi
siscenter
ali
gned
Cor
eAt
tri
but
es
Thef
ourcor
eat
tri
but
est
hatcanbeusedont
hemaj
ori
tyofHTMLel
ement
s
(
alt
houghnotal
l
)ar
e:
I
d
Ti
tl
e
Cl
ass
St
yle
TheI
dAt
tri
but
e
Thei
dat
tri
but
eofanHTMLt
agcanbeusedt
ouni
quel
yident
if
yanyel
ement
wi
thi
nanHTMLpage.Ther
ear
etwopr
imar
yreasonst
haty
oumi
ghtwantt
ouseani
d
at
tri
but
eonanel
ement
:
NehaMi
dha,
Lect
urer GPPanchkul
a
I
fanel
ementcar
ri
esani
dat
tr
ibut
easauni
quei
dent
if
ier
,iti
spossi
blet
oident
if
y
j
ustt
hatel
ementandi
tscont
ent
.
I
fyouhav
etwoel
ement
soft
hesamenamewi
thi
naWebpage(
orst
ylesheet
),
y
oucanuset
hei
dat
tri
but
etodi
sti
ngui
shbet
weenel
ement
sthathav
ethe
samename.
<pi
d="html">ThisparaexplainswhatisHTML</p>
<pi
d="
c ss"
>Th ispar
ae xpl
ainswhatisCascadi
ngSt y
leSheet
</p>
Thet
it
leAt
tri
but
e
Theti
tl
eat
tri
but
egiv
esasuggest
edti
tl
efort
heel
ement
.Theysy
ntaxf
ort
het
it
le
at
tri
but
eissimi
l
arasexpl
ainedf
ori
datt
ri
bute:
Thebehavi
orofthi
satt
ri
but
ewil
ldependupontheel
ementt
hatcar
ri
esit,
alt
hough
i
tisoft
endispl
ayedasatool
ti
pwhencursorcomesovert
heel
ementorwhil
ethe
el
ementisl
oading.
Exampl
e
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>Thet
it
leAt
tri
but
eExampl
e</
ti
tl
e>
</
head>
<body
>
<h3t
it
le="
Hel
l
oHTML!
">Ti
tl
edHeadi
ngTagExampl
e</
h3>
</
body
>
</
html>
Thi
swi
l
lpr
oducet
hefol
lowingr
esul
t:
Ti
tl
edHeadi
ngTagExampl
e
Nowt r
ytobri
ngyourcursorover"
Tit
ledHeadi
ngTagExample"andyouwillsee
t
hatwhat
evert
it
leyouusedinyourcodeiscomingoutasat
oolt
ipofthecursor
.
Thecl
assAt
tri
but
e
Theclassattr
ibut
eisusedtoassoci
ateanelementwithast
ylesheet,and
speci
fi
estheclassofelement
.Youwilll
ear
nmor eabouttheuseoftheclassatt
ri
but
e
whenyouwi l
ll
earn
NehaMi
dha,
Lect
urer GPPanchkul
a
Cascadi
ngSt
yleSheet(
CSS)
.Sof
ornowy
oucanav
oidi
t.
Thev
alueoft
heat
tri
but
emayal
sobeaspace-
separ
atedl
i
stofcl
assnames.Forexampl
e:
cl
ass="
className1className2className3"
Thest
yleAt
tri
but
e
Thest
yleat
tri
but
eal
l
owsy
out
ospeci
fyCascadi
ngSt
yleSheet(
CSS)r
uleswit
hint
he
el
ement.
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>Thesty
l eAt
tr
ibut
e</
tit
le>
</
head>
<body
>
<pst
yle="
font
-f
ami
l
y:
ar
ial
;col
or
:#FF0000;
"
>Somet
ex
t .
..
</
p>
</
body
>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t:
Somet
ext
..
.
HTMLFor mat t
ing
Ifyouuseawor dprocessor
,youmustbefamil
i
arwiththeabil
i
tytomaketext
bol
d,i
tali
cized,orunder
li
ned;
thesearej
ustt
hreeoft
hetenopt
ionsavai
labl
etoi
ndi
cate
howtextcanappeari nHTMLandXHTML.
Bol
dText
Any
thi
ngt
hatappear
swi
thi
n<b>.
..
</b>el
ement
,isdi
spl
ayedi
nbol
dasshownbel
ow:
<!DOCTYPEht ml>
<html>
<head>
<tit
le>BoldTextExampl
e</t
it
le>
</head>
<body>
<p>Thef oll
owi
ngwor dusesa<b>bol
d</
b>t
ypef
ace.
</
p>
</body >
NehaMi
dha,
Lect
urer GPPanchkul
a
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t:
Thef
oll
owi
ngwor
dusesabol
dty
pef
ace.
I
tal
icText
Any
thi
ngt
hatappear
swi
thi
n<i
>..
.</
i>el
ementi
sdi
spl
ayedi
nit
ali
cized.
Under
li
neText
Any
thi
ngt
hatappear
swi
thi
n<u>.
..
</u>el
ement
,isdi
spl
ayedwi
thunder
li
ne.
Super
scr
iptText
Thecont
entofa<sup>.
..
</sup>el
ementi
swr
it
teni
nsuper
scr
ipt
;thef
ontsi
ze
usedi
sthesamesi
zeast
hechar
act
erssur
roundi
ngi
tbuti
sdi
spl
ayedhal
fachar
act
er'
s
hei
ghtabov
etheot
herchar
act
ers.
Subscr
iptText
Thecont
entofa<sub>.
..
</sub>el
ementi
swr
it
teni
nsubscr
ipt
;thef
ontsi
zeused
i
sthesameast
hechar
act
erssur
roundi
ngi
t,buti
sdi
spl
ayedhal
fachar
act
er'
shei
ght
beneat
htheot
herchar
act
ers.
Font
sinHTML
Font
spl
ayav
eryi
mpor
tantr
olei
nmaki
ngawebsi
temor
euserf
ri
endl
yand
i
ncr
easi
ngcont
entr
eadabi
l
ity
.Fontf
aceandcol
ordependsent
ir
elyont
hecomput
er
andbr
owsert
hati
sbei
ngusedt
ovi
ewy
ourpagebuty
oucanuseHTML<f
ont
>tagt
o
addst
yle,si
ze,andcol
ort
othet
extony
ourwebsi
te.Youcanusea<basef
ont
>tagt
o
setal
lofy
ourt
extt
othesamesi
ze,
face,
andcol
or.
Thef
ontt
agi
shav
ingt
hreeat
tri
but
escal
l
edsi
ze,col
or,andf
acet
ocust
omi
ze
y
ourf
ont
s.Tochangeanyoft
hef
ontat
tri
but
esatanyt
imewi
thi
nyourwebpage,
simpl
y
uset
he<f
ont
>tag.Thet
extt
hatf
oll
owswi
l
lremai
nchangedunt
ily
oucl
osewi
tht
he
</
font
>tag.Youcanchangeoneoral
loft
hef
ontat
tri
but
eswi
thi
none<f
ont
>tag.
SetFontSi
ze
NehaMi
dha,
Lect
urer GPPanchkul
a
Youcansetcont
entf
ontsi
zeusi
ngsi
zeat
tri
but
e.Ther
angeofaccept
edv
aluesi
sfr
om
1(
smal
l
est
)to7(
lar
gest
).Thedef
aul
tsi
zeofaf
onti
s3.
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>Set
ti
ngFontSi
ze</
ti
tl
e>
</head>
<body>
<f
ontsi
ze="
1">Fontsi
ze="
1"</
font
><br/
>
<f
ontsi
ze="
2">Fontsi
ze="
2"</
font
><br/
>
<f
ontsi
ze="
3">Fontsi
ze="
3"</
font
><br/
>
<f
ontsi
ze="
4">Fontsi
ze="
4"</
font
><br/
>
<f
ontsi
ze="
5">Fontsi
ze="
5"</
font
><br/
>
<f
ontsi
ze="
6">Fontsi
ze="
6"</
font
><br/
>
<f
ontsi
ze="
7">Fontsi
ze="
7"</
font
>
</body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
Fontsi
ze="
1"
Fontsi
ze="
2"
Fontsi
ze=
"
3"Fontsi
ze
="
4"
Fontsi
ze="
5"
Fontsi
ze="
6"
Fontsi
ze="
7"
Set
ti
ngFontCol
or
Youcansetanyf
ontcol
ory
oul
i
keusi
ngcol
orat
tri
but
e.Youcanspeci
fyt
he
col
ort
haty
ouwantbyei
thert
hecol
ornameorhexadeci
mal
codef
ort
hatcol
or.
<!
DOCTYPEht
ml>
NehaMi
dha,
Lect
urer GPPanchkul
a
<ht
ml>
<head>
<t
it
le>Set
ti
ngFontCol
or</
ti
tl
e>
</head>
<body>
<f
ontcol
or="
#FF00FF"
>Thistextisi
npink</
font
><br/
>
<f
ontcol
or="
red"
>Thi
stextisred</f
ont
>
</body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
Thi
stexti
sin
pi
nkThi
stexti
s
r
ed
Comment
sinHTML
Commenti
sapi
eceofcodewhi
chi
signor
edbyanywebbr
owser
.Iti
sagood
pr
act
icet
oaddcomment
sint
oyourHTMLcode,especi
all
yincompl
exdocument
s,t
o
i
ndi
cat
esect
ionsofadocument
,andanyot
hernot
est
oany
onel
ooki
ngatt
hecode.
Comment
shel
pyouandot
her
sunder
standy
ourcodeandi
ncr
easescoder
eadabi
l
ity
.
HTMLcomment
sar
epl
acedi
nbet
ween<!
--.
..-
->t
ags.So,
anycont
entpl
acedwi
th-
in
<!
--.
..-
->t
agswi
l
lbet
reat
edascommentandwi
l
lbecompl
etel
yignor
edbyt
hebr
owser
.
<!
DOCTYPEht
ml>
<ht
ml>
<body >
<p>Documentcont
entgoesher
e </
p>
</body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
twi
thoutdi
spl
ayi
ngt
hecont
entgi
venasapar
tof
comment
s–
Documentcont
entgoesher
e..
..
.
NehaMi
dha,
Lect
urer GPPanchkul
a
HTMLI
mages
I
magesar
ever
yimpor
tantt
o beaut
if
yaswel
last
o depi
ctmanycompl
ex
concept
sinsi
mpl
ewayony
ourwebpage.Thi
stut
ori
alwi
l
ltakey
out
hroughsi
mpl
e
st
epst
ousei
magesi
nyourwebpages.
I
nser
tImage
Youcani
nser
tanyi
magei
nyourwebpagebyusi
ng<i
mg>t
ag.Fol
l
owi
ngi
sthe
si
mpl
esy
ntaxt
ouset
hist
ag.
<i
mgsr
c="
ImageURL".
..at
tri
but
es-
li
st/
>
The<i
mg>t
agi
sanempt
ytag,
whi
chmeanst
hat
,itcancont
ainonl
yli
stofat
tri
but
es
andi
thasnocl
osi
ngt
ag.
Exampl
e
Tot
ryf
oll
owi
ngexampl
e,l
et'
skeepourHTMLf
il
etest
.ht
m andi
magef
il
e
t
est
.pngi
nthesamedi
rect
ory–
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>Usi
ngI
magei
nWebpage</
ti
tl
e>
</head>
<body >
<p>Si
mpleI
mageInser
t</
p>
<i
mgsr c="
/ht
ml/
images/test
.png"al
t="
TestI
mage"/
>
</body>
</
html
>
YoucanusePNG,
JPEGorGI
Fimagef
il
ebasedony
ourcomf
ortbutmakesur
e
y
ouspeci
fycor
recti
magef
il
enamei
nsr
cat
tri
but
e.I
magenamei
sal
way
scase
sensi
ti
ve.
Theal
tat
tri
but
eisamandat
oryat
tri
but
ewhi
chspeci
fi
esanal
ter
nat
etextf
oran
i
mage,
ift
hei
magecannotbedi
spl
ayed.
SetI
mageLocat
ion
NehaMi
dha,
Lect
urer GPPanchkul
a
Usual
l
ywekeepal
lthei
magesi
nasepar
atedi
rect
ory
.Sol
et'
skeepHTMLf
il
e
t
est
.ht
m i
nourhomedi
rect
oryandcr
eat
easubdi
rect
oryi
magesi
nsi
det
hehome
di
rect
orywher
ewewi
l
lkeepouri
maget
est
.png.
Exampl
e
Assumi
ngouri
magel
ocat
ioni
s"i
mage/
test
.png"
,tr
ythef
oll
owi
ngexampl
e–
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>SetI
mageBor
der
</t
it
le>
</head>
<body >
<p>Set
ti
ngi
mageBorder
</p>
<i
mgsr c="
/ht
ml/
images/t
est
.png"al
t="
TestI
mage"bor
der
="
3"/>
</body>
</
html
>
SetI
mageWi
dth/
Hei
ght
Youcanseti
magewi
dthandhei
ghtbasedony
ourr
equi
rementusi
ngwi
dthandhei
ght
at
tri
but
es.Youcanspeci
fywi
dthandhei
ghtoft
hei
magei
nter
msofei
therpi
xel
sor
per
cent
ageofi
tsact
ual
size.
Exampl
e
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>SetI
mageWi
dthandHei
ght
</t
it
le>
</head>
<body >
<p>Set
ti
ngi
magewidt
handheight</p>
<i
mgsr c="
/ht
ml/
images/
test
.png"alt="
TestI
mage"wi
dth="
150"hei
ght=
"
100"/>
</body>
</
html
>
NehaMi
dha,
Lect
urer GPPanchkul
a
SetI
mageBor
der
Bydef
aul
t,i
magewi
l
lhav
eabor
derar
oundi
t,y
oucanspeci
fybor
der
t
hicknessi
nter
msofpi
xel
susi
ngbor
derat
tri
but
e.At
hicknessof0means,
nobor
der
ar
oundt
hepi
ctur
e.
Exampl
e
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>SetI
mageBor
der
</t
it
le>
</head>
<body >
<p>Set
ti
ngi
mageBorder
</p>
<i
mgsr c="
/ht
ml/
images/t
est
.png"al
t="
TestI
mage"bor
der
="
3"/>
</body>
</
html
>
SetI
mageAl
ignment
Bydef
aul
t,i
magewi
l
lal
i
gnatt
hel
eftsi
deoft
hepage,
buty
oucanuseal
i
gn
at
tri
but
etoseti
tint
hecent
erorr
ight
.
Exampl
e
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>SetI
mageAl
i
gnment
</t
it
le>
</head>
<body >
<p>Sett
ingimageAli
gnment</
p>
<i
mgsr c=" /
html/
images/
test
.png"al
t="
TestI
mage"bor
der
="
3"al i
gn=" ri
ght"
/>
</body>
</
html
>
I
mageMappi
ng
TheHTML<map>t
agi
susedf
ordef
ini
ngani
magemapal
ongwi
th<i
mg>t
ag.
NehaMi
dha,
Lect
urer GPPanchkul
a
Exampl
e
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>HTMLmapTag</
ti
tl
e>
</head>
<body
>
<i
mgsr
c="
/i
mages/
html
.gi
f"al
t="
HTMLMap"bor
der="
0"usemap="
#ht
ml"
/>
<!
--Create Mappi
ngs-->
<mapname=" html
">
<areashape="ci
rcl
e"coords="154,
150,
59"hr
ef=
"
about/
about_team.ht
m"
al
t="Team"tar
get="_sel
f"/>
</
map>
</
body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t,f
indt
hei
magemaponbot
tom r
ight
.
HTMLTabl
e
TheHTMLt
abl
esal
l
owwebaut
hor
stoar
rangedat
ali
ket
ext
,images,
li
nks,
other
t
abl
es,
etc.i
ntor
owsandcol
umnsofcel
l
s.
TheHTMLt
abl
esar
ecr
eat
edusi
ngt
he<t
abl
e>t
agi
nwhi
cht
he<t
r>t
agi
sused
t
ocr
eat
etabl
erowsand<t
d>t
agi
susedt
ocr
eat
edat
acel
l
s.Theel
ement
sunder<t
d>
ar
eregul
arandl
eftal
i
gnedbydef
aul
t
Exampl
e
<!
DOCTYPEht
ml>
NehaMi
dha,
Lect
urer GPPanchkul
a
<ht
ml>
<head>
<t
it
le>HTMLTabl
es</
ti
tl
e>
</head>
<body
>
<t
ableborder="1"
>
<tr
>
<td>Row1,Col
umn1</
td>
<td>Row1,Col
umn2</
td>
</t
r>
<t
r>
<td>Row2,
Col
umn1</
td>
<td>Row2,
Col
umn2</
td>
</
tr>
</t
able>
</
body
>
</
html>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
Row1,
Col
umn Row1,
Col
umn
1 2
Row2,
Col
umn Row2,
Col
umn
1 2
Her
e,t
hebor
deri
sanat
tri
but
eof<t
abl
e>t
agandi
tisusedt
oputabor
deracr
oss
al
lthecel
l
s.I
fyoudonotneedabor
der
,theny
oucanusebor
der="
0".
Tabl
eHeadi
ng
Tabl
eheadi
ngcanbedef
inedusi
ng<t
h>t
ag.Thi
stagwi
l
lbeputt
orepl
ace<t
d>
t
ag,whi
chi
susedt
orepr
esentact
ualdat
acel
l
.Nor
mal
l
yyouwi
l
lputy
ourt
opr
ow as
t
abl
e headi
ng as shown bel
ow,ot
her
wise y
ou can use <t
h>el
ementi
n anyr
ow.
Headi
ngs,
whi
char
edef
inedi
n
<t
h>t
ag ar
e cent
ered and bol
d by
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>HTMLTabl
eHeader
</t
it
le>
</head>
<body
>
<t
abl
ebor
der="
1">
def
aul
t.Exampl
e
NehaMi
dha,
Lect
urer GPPanchkul
a
<t
r>
<th>Name</th>
<th>Sal
ary</
th>
</
tr>
<t
r>
<td>RameshRaman</
td>
<td>5000</t
d>
</
tr>
<t
r>
<td>Shabbi
rHussei
n</
td>
<td>7000</
td>
</
tr>
</t
able>
</
body
>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
Name Salar
y
Ramesh 5000
Raman
Shabbi
r 7000
Hussei
n
Cel
lpaddi
ngandCel
lspaci
ngAt
tri
but
es
Ther
ear
etwoat
tri
but
escal
l
edcel
l
paddi
ngandcel
l
spaci
ngwhi
chy
ouwi
l
luset
o
adj
ustt
hewhi
tespacei
nyourt
abl
ecel
l
s.Thecel
l
spaci
ngat
tri
but
edef
inesspace
bet
weent
abl
ecel
l
s,whi
l
ecel
l
paddi
ngr
epr
esent
sthedi
stancebet
weencel
lbor
der
sand
t
hecont
entwi
thi
nacel
l
.
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>HTMLTabl
eCel
l
paddi
ng</
ti
tl
e>
</head>
<body
>
<t
ableborder="1"cellpaddi
ng="5"cel
l
spaci
ng="
5">
<tr
>
<th>Name</ th>
<th>Sal
ary</th>
</t
r>
<tr
>
<td>RameshRaman</ td>
<td>5000</td>
</t
r>
<tr
>
<td>ShabbirHussein</
td>
Exampl
e
NehaMi
dha,
Lect
urer GPPanchkul
a
<td>7000</
td>
</
tr>
</
table>
</
body
>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
Name Sal
ary
Ramesh 5000
Raman
Shabbi
r 7000
Hussei
n
Col
spanandRowspanAt
tri
but
es
Youwi
l
lusecol
spanat
tri
but
eify
ouwantt
omer
get
woormor
ecol
umnsi
ntoa
si
ngl
ecol
umn.Si
mil
arwayy
ouwi
l
luser
owspani
fyouwantt
omer
get
woormor
e
r
ows.
Exampl
e
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>HTMLTabl
eCol
span/
Rowspan</
ti
tl
e>
</head>
<body>
<t
ableborder="1">
<tr
>
<th>Column1</th>
<th>Column2</th>
<th>Column3</th>
</t
r>
<tr
>
<tdrowspan=" 2">Row1Cel
l1</
td>
<td>Row1Cell 2</td>
<td>Row1Cell 3</td>
</t
r>
<tr
>
<td>Row2Cell 2</td>
<td>Row2Cell 3</td>
</t
r>
<tr
>
<tdcolspan="3">Row3Cell
1</t
d>
</t
r>
</t
able>
</body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
NehaMi
dha,
Lect
urer GPPanchkul
a
Col
umn1 Column2 Column3
Row1Cel
l2Row1Cel
l3
Row1Cel
l1
Row2Cel
l2Row2Cel
l3
Row3Cel
l1
Tabl
eHei
ghtandWi
dth
Youcansetat
abl
ewi
dthandhei
ghtusi
ngwi
dthandhei
ghtat
tri
but
es.Youcan
speci
fyt
abl
ewi
dthorhei
ghti
nter
msofpi
xel
sori
nter
msofper
cent
ageofav
ail
abl
e
scr
eenar
ea.
Exampl
e
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>HTMLTabl
eWi
dth/
Hei
ght
</t
it
le>
</head>
<body
>
<t
ableborder="1"wi
dth="400"hei
ght="
150"
>
<tr
>
<td>Row1,Column1</td>
<td>Row1,Column2</td>
</t
r>
<t
r>
<td>Row2,
Col
umn1</
td>
<td>Row2,
Col
umn2</
td>
</
tr>
</t
able>
</
body
>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
Row1,
Col
umn1 Row1,
Col
umn2
Row2,
Col
umn1 Row2,
Col
umn2
Tabl
eCapt
ion
Thecapt
iont
agwi
l
lser
veasat
it
leorexpl
anat
ionf
ort
het
abl
eandi
tshowsupat
t
het
opoft
het
abl
e.Thi
stagi
sdepr
ecat
edi
nnewerv
ersi
onofHTML/
XHTML.
Exampl
e
<!
DOCTYPEht
ml>
NehaMi
dha,
Lect
urer GPPanchkul
a
<ht
ml>
<head>
<t
it
le>HTMLTabl
eCapt
ion</
ti
tl
e>
</head>
<body
>
<t
ableborder="1"width="100%">
<capt
ion>Thisi
st hecapt
ion</
capt
ion>
<t
r>
<td>r
ow1,
col
umn1</
td><t
d>r
ow1,
col
umnn2</
td>
</
tr>
<t
r>
<td>r
ow2,
col
umn1</
td><t
d>r
ow2,
col
umnn2</
td>
</
tr>
</t
able>
</
body
>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
Thi
sisthe
capt
ion
r
ow1,
col
umn1 r
ow1, col
umn2
r
ow2,
col
umn1 r
ow2, col
umn2
Hy
per
li
nki
nHTML
Awebpagecancont
ainv
ari
ousl
i
nkst
hatt
akey
oudi
rect
lyt
oot
herpagesand
ev
enspeci
fi
cpar
tsofagi
venpage.Thesel
i
nksar
eknownashy
per
li
nks.
Hy
per
li
nksal
l
ow v
isi
tor
stonav
igat
ebet
weenWebsi
tesbycl
i
cki
ngonwor
ds,
phr
ases,andi
mages.Thusy
oucancr
eat
ehy
per
li
nksusi
ngt
extori
magesav
ail
abl
eon
awebpage.
Li
nki
ngDocument
s
Al
i
nki
sspeci
fi
edusi
ngHTMLt
ag<a>.Thi
stagi
scal
l
edanchort
agandany
thi
ng
bet
weent
heopeni
ng<a>t
agandt
hecl
osi
ng</
a>t
agbecomespar
toft
hel
i
nkanda
usercancl
i
ckt
hatpar
ttor
eacht
othel
i
nkeddocument
.Fol
l
owi
ngi
sthesi
mpl
esy
ntax
t
ouse<a>t
ag.
<a hr
ef = "
Document URL".
..at
tri
but
es-
li
st>Li
nk
Text
</a>Exampl
e
NehaMi
dha,
Lect
urer GPPanchkul
a
Let
'st
ryf
oll
owi
ngex
ampl
ewhi
chl
i
nksht
tp:
//www.
gpwf
ari
dabad.
edu.
inaty
ourpage–
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>Hy
per
li
nkExampl
e</
ti
tl
e>
</head>
<body >
<p>Cli
ckf
oll
owi
ngli
nk</
p>
<ahref="
htt
ps:
//www.gpwfar
idabad.
edu.
in"t
arget="
_sel
f"
>Gov
t.
Pol
ytechnicforWomen,
Fari
dabad</
a>
</body>
</
html
>
HTMLFor
ms
HTMLFor
msar
erequi
red,wheny
ouwantt
ocol
l
ectsomedat
afr
om t
hesi
te
v
isi
tor
.Forexampl
e,dur
inguserr
egi
str
ati
ony
ouwoul
dli
ket
ocol
l
ecti
nfor
mat
ionsuch
asname,
emai
laddr
ess,
credi
tcar
d,et
c.
Af
orm wi
l
ltakei
nputf
rom t
hesi
tev
isi
torandt
henwi
l
lposti
ttoaback-
end
appl
i
cat
ionsuchasCGI
,ASPScr
iptorPHPscr
iptet
c.Theback-
endappl
i
cat
ionwi
l
l
per
for
mrequi
redpr
ocessi
ngont
hepasseddat
abasedondef
inedbusi
nessl
ogi
cinsi
de
t
heappl
i
cat
ion.
Ther
ear
evar
iousf
orm el
ement
sav
ail
abl
eli
ket
extf
iel
ds,t
ext
areaf
iel
ds,dr
op-
downmenus,
radi
obut
tons,
checkboxes,
etc.
TheHTML<f
orm>t
agi
susedt
ocr
eat
eanHTMLf
orm andi
thasf
oll
owi
ngsy
ntax–
<form acti
on=" Scri
ptURL"met hod="GET|POST"
>
for
m elementsli
keinput,t
extar
eaetc.
</ f
orm>
Form At
tri
but
es
Apar
tfrom commonat t
ri
butes,f
oll
owingisali
stofthemostf
requent
ly
usedfor
m att
ributes−
Sr
.No At
tri
but
e&Descr
ipt
ion
1
act
ion
Backendscr
iptr
eadyt
opr
ocessy
ourpasseddat
a.
NehaMi
dha,
Lect
urer GPPanchkul
a
2
met
hod
Methodt
obeusedt
oupl
oaddat
a.Themostf
requent
lyusedar
eGETand
POSTmethods.
3
t
arget
Specif
ythetargetwindoworf
ramewheretheresul
tofthescr
iptwi
l
lbe
di
splayed.I
ttakesvaluesl
i
ke_bl
ank,
_sel
f,_par
entetc.
4
enct
ype
Youcanusetheenct
ypeat
tri
butetospecif
yhowthebr
owserencodest
hedat
a
bef
orei
tsendsitt
otheser
ver.Possi
bleval
uesar
e−
appl
icat
ion/
x-www-f
orm-ur
lencoded−Thi
sist
hest
andar
dmet
hodmostf
orms
usei
nsimplescenar
ios.
mutl
ipar
t/f
orm-dat
a−Thisi
susedwheny
ouwantt
oupl
oadbi
nar
ydat
aint
he
f
orm offi
l
esli
keimage,
wordfi
l
eetc.
HTMLFor
m Cont
rol
s
Ther
ear
edi
ff
erentt
ypesoff
orm cont
rol
sthaty
oucanuset
ocol
l
ectdat
ausi
ngHTMLf
orm−
TextI
nputCont
rol
s
CheckboxesCont
rol
s
Radi
oBoxCont
rol
s
Sel
ectBoxCont
rol
s
Fi
l
eSel
ectboxes
Hi
ddenCont
rol
s
Cl
i
ckabl
eBut
tons
Submi
tandResetBut
ton
TextI
nputCont
rol
s
Ther
ear
ethr
eet
ypesoft
exti
nputusedonf
orms−
Si
ngl
e-l
inet
exti
nputcont
rol
s−Thi
scont
rol
isusedf
ori
temst
hatr
equi
reonl
y
onel
i
neofuseri
nput
,suchassear
chboxesornames.Theyar
ecr
eat
edusi
ng
HTML<i
nput
>tag.Her
eisabasi
cexampl
eofasi
ngl
e-l
i
net
exti
nputusedt
otake
f
ir
stnameandl
astname–
NehaMi
dha,
Lect
urer GPPanchkul
a
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>TextI
nputCont
rol
</t
it
le>
</head>
<body>
<f
orm>
Fir
stname:<i
nputt
ype="
text
"name="
fir
st_
name"/>
<br>
Lastname:
<inputt
ype="
tex
t"name="
last
_name"/
>
</
form>
</body>
</
html
>
Fi
rst
name:
Lastname:
At
tri
but
es
Fol
l
owi
ngi
sthel
i
stofat
tri
but
esf
or<i
nput
>tagf
orcr
eat
ingt
extf
iel
d.
Sr
.No At
tri
but
e&Descr
ipt
ion
1
t
ype
I
ndi
cat
est
het
ypeofi
nputcont
rol
andf
ort
exti
nputcont
rol
itwi
l
lbesett
otext
.
2
name
Usedtogi
veanamet
othecont
rol
whi
chi
ssentt
otheser
vert
ober
ecogni
zedand
gett
heval
ue.
3
v
alue
Thi
scanbeusedt
opr
ovi
deani
nit
ial
val
uei
nsi
det
hecont
rol
.
4
si
ze
Al
l
owst
ospeci
fyt
hewi
dthoft
het
ext
-i
nputcont
rol
int
ermsofchar
act
ers.
NehaMi
dha,
Lect
urer GPPanchkul
a
5
maxl
engt
h
All
owst
ospeci
fyt
hemaxi
mum numberofchar
act
ersausercanent
eri
ntot
het
ext
box.
Passwor
dinputcont
rol
s−Thi
sisal
soasi
ngl
e-l
i
net
exti
nputbuti
tmaskst
he
char
act
erassoonasauserent
ersi
t.Theyar
eal
socr
eat
edusi
ngHTMl
<input
>
t
ag.
Her
eisabasi
cexampl
eofasi
ngl
e-l
i
nepasswor
dinputusedt
otakeuserpasswor
d–
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>Passwor
dInputCont
rol
</t
it
le>
</head>
<body>
<f
orm>
UserI
D:<inputtype="text
"name="user
_id"/>
<br
>
Passwor
d:<inputtype="passwor
d"name=" passwor
d"/
>
</
form>
</body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
User
ID:
Passwor
d:
At
tri
but
es
Fol
l
owi
ngi
sthel
i
stofat
tri
but
esf
or<i
nput
>tagf
orcr
eat
ingpasswor
dfi
eld.
Sr
.No At
tri
but
e&Descr
ipt
ion
1
t
ype
I
ndi
cat
est
het
ypeofi
nputcont
rol
andf
orpasswor
dinputcont
rol
itwi
l
lbeset
NehaMi
dha,
Lect
urer GPPanchkul
a
t
opasswor
d.
2
name
Usedtogi
veanamet
othecont
rol
whi
chi
ssentt
otheser
vert
ober
ecogni
zedand
gett
heval
ue.
3
v
alue
Thi
scanbeusedt
opr
ovi
deani
nit
ial
val
uei
nsi
det
hecont
rol
.
4
si
ze
Al
l
owst
ospeci
fyt
hewi
dthoft
het
ext
-i
nputcont
rol
int
ermsofchar
act
ers.
5
maxl
engt
h
All
owst
ospeci
fyt
hemaxi
mum numberofchar
act
ersausercanent
eri
ntot
het
ext
box.
Mul
ti
-l
inet
exti
nputcont
rol
s−Thi
sisusedwhent
heuseri
srequi
redt
ogi
ve
det
ail
sthatmaybel
ongert
hanasi
ngl
esent
ence.Mul
ti
-l
inei
nputcont
rol
sar
e
cr
eat
edusi
ngHTML<t
ext
area>t
ag.
Her
eisabasi
cexampl
eofamul
ti
-l
inet
exti
nputusedt
otakei
tem descr
ipt
ion–
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>Mul
ti
ple-
LineI
nputCont
rol
</t
it
le>
</head>
<body>
<f
orm>
Descr i
ption:<br/
>
<textarearows="5"col
s="
50"name="
descr
ipt
ion"
>Ent
erdescr
ipt
ion
here...
</textarea>
</
form>
</body>
</
html
>
NehaMi
dha,
Lect
urer GPPanchkul
a
Descr
ipt
ion:
At
tri
but
es
Fol
l
owi
ngi
sthel
i
stofat
tri
but
esf
or<t
ext
area>t
ag.
Sr
.No At
tri
but
e&Descr
ipt
ion
1
name
Usedtogi
veanamet
othecont
rol
whi
chi
ssentt
otheser
vert
ober
ecogni
zed
andgett
heval
ue.
2
r
ows
I
ndi
cat
est
henumberofr
owsoft
extar
eabox.
3
col
s
I
ndi
cat
est
henumberofcol
umnsoft
extar
eabox
CheckboxCont
rol
Checkboxesar
eusedwhenmor
ethanoneopt
ioni
srequi
redt
obesel
ect
ed.They
ar
eal
socr
eat
edusi
ngHTML<i
nput
>tagbutt
ypeat
tri
but
eissett
ocheckbox.
.
Exampl
e
Her
eisanexampl
eHTMLcodef
oraf
orm wi
tht
wocheckboxes–
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>CheckboxCont
rol
</t
it
le>
</head>
<body
>
NehaMi
dha,
Lect
urer GPPanchkul
a
<f
orm>
<i
nputt
ype="
checkbox
"name="
mat
hs"v
alue="
on"
>
Mat
hs
<i
nputt
ype="
checkbox
"name="
phy
sics"v
alue="
on"
>
Phy
sics
</
form>
</body>
</ht
ml>
Thiswi
llpr
oducet
hef
oll
owi
ngr
esul
t–
Mat
hs Phy
sics
At
tri
but
es
Fol
l
owi
ngi
sthel
i
stofat
tri
but
esf
or<checkbox>t
ag.
Sr
.No At
tri
but
e&Descr
ipt
ion
1
t
ype
I
ndi
cat
esthet y
peofi nputcont
rolandf
orcheckboxi
nputcont
roli
twi
l
l
besettocheckbox.
2
name
Usedtogi
veanamet
othecont
rol
whi
chi
ssentt
otheser
vert
ober
ecogni
zed
andgett
heval
ue.
3
v
alue
Thev
aluet
hatwi
l
lbeusedi
fthecheckboxi
ssel
ect
ed.
4
checked
ocheckedi
Sett fyouwantt
osel
ecti
tbydef
aul
t.
Radi
oBut
tonCont
rol
Radiobut
tonsareusedwhenoutofmanyopt
ions,j
ustoneopti
onisrequi
redt
o
beselect
ed.Theyareal
socreat
edusi
ngHTML<input>tagbuttypeat
tri
but
eissetto
radi
o.
NehaMi
dha,
Lect
urer GPPanchkul
a
Exampl
e
Her
eisexampl
eHTMLcodef
oraf
orm wi
tht
wor
adi
obut
tons−
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>Radi
oBoxCont
rol
</t
it
le>
</head>
<body>
<f
orm>
<i
nputt
ype="r
adi
o"name="subject"value="mat
hs">Maths
<i
nputt
ype="
radi
o"name="
subject"value="
physi
cs"
>Physics
</
form>
</body>
</
html>
Thiswil
lpr
oducet
hef
oll
owi
ngr
esul
t−
Mat
hs Phy
sics
At
tri
but
es
Fol
l
owi
ngi
sthel
i
stofat
tri
but
esf
orr
adi
obut
ton.
Sr
.No At
tri
but
e&Descr
ipt
ion
1
t
ype
I
ndi
cat
est
het
ypeofi
nputcont
rol
andf
orcheckboxi
nputcont
rol
itwi
l
lbesett
o
r
adi
o.
2
name
Usedtogi
veanamet
othecont
rol
whi
chi
ssentt
otheser
vert
ober
ecogni
zed
andgett
heval
ue.
3
v
alue
Thev
aluet
hatwi
l
lbeusedi
fther
adi
oboxi
ssel
ect
ed.
4
checked
NehaMi
dha,
Lect
urer GPPanchkul
a
ocheckedi
Sett fyouwantt
osel
ecti
tbydef
aul
t.
Sel
ectBoxCont
rol
Aselectbox,al
socalleddropdownboxwhichprov
idesopt
iont
oli
stdownvar
ious
opt
ionsintheform ofdropdownli
st,
from wher
eausercansel
ectoneormor
e
opt
ions.
Exampl
e
Her
eisexampl
eHTMLcodef
oraf
orm wi
thonedr
opdownbox
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<tit
le>Sel
ectBoxCont
rol
</t
it
le>
</head>
<body>
<form>
<selectname="
dropdown" >
<opt i
onval
ue="Maths"select
ed>Maths</opt
ion>
<opt i
onval
ue="Physi
cs">Physi
cs</
option>
</select
>
</form>
</body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
At
tri
but
es
Fol
l
owi
ngi
sthel
i
stofi
mpor
tantat
tr
ibut
esof<sel
ect
>tag−
Sr
.No At
tri
but
e&Descr
ipt
ion
1
name
NehaMi
dha,
Lect
urer GPPanchkul
a
Usedtogi
veanamet
othecont
rol
whi
chi
ssentt
otheser
vert
ober
ecogni
zed
andgett
heval
ue.
2
si
ze
Thi
scanbeusedt
opr
esentascr
oll
i
ngl
i
stbox.
3
mul
ti
ple
I
fsett
o"mul
ti
ple"t
henal
l
owsausert
osel
ectmul
ti
plei
temsf
rom t
hemenu.
Fol
l
owi
ngi
sthel
i
stofi
mpor
tantat
tr
ibut
esof<opt
ion>t
ag−
Sr
.No At
tri
but
e&Descr
ipt
ion
1
v
alue
Thev
aluet
hatwi
l
lbeusedi
fanopt
ioni
nthesel
ectboxboxi
ssel
ect
ed.
2
sel
ect
ed
Speci
fi
est
hatt
hisopt
ionshoul
dbet
hei
nit
ial
l
ysel
ect
edv
aluewhent
hepage
l
oads.
3
l
abel
Anal
ter
nat
ivewayofl
abel
i
ngopt
ions
Fi
leUpl
oadBox
Ifyouwanttoall
owausertouploadaf i
letoyourwebsit
e,y
ouwi l
lneedt
ouseaf i
l
e
uploadbox,al
soknownasaf i
l
esel ectbox.Thi
sisalsocreat
edusingthe<i
nput
>
elementbutt
ypeatt
ri
but
eissett
of i
le.
Exampl
e
Her
eisexampl
eHTMLcodef
oraf
orm wi
thonef
il
eupl
oadbox−
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<tit
le>Fi
l
eUpl
oadBox</
ti
tl
e>
</head>
NehaMi
dha,
Lect
urer GPPanchkul
a
<body>
<form>
<i
nputt
ype="
fi
le"name="
fi
leupl
oad"accept="
image/
*"/
>
</for
m>
</body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
At
tri
but
es
Fol
l
owi
ngi
sthel
i
stofi
mpor
tantat
tri
but
esoff
il
eupl
oadbox−
Sr
.No At
tri
but
e&Descr
ipt
ion
1
name
Usedtogi
veanamet
othecont
rol
whi
chi
ssentt
otheser
vert
ober
ecogni
zed
andgett
heval
ue.
2
accept
Speci
fi
est
het
ypesoff
il
est
hatt
heser
veraccept
s.
But
tonCont
rol
s
Therearevar
iouswaysinHTMLt ocreat
ecli
ckabl
ebut
tons.Youcanalsocr
eat
ea
cl
ickabl
ebutt
onusing<input
>tagbysett
ingit
st y
peatt
ri
butetobutt
on.Thetype
at
tribut
ecant
akethef
oll
owingval
ues−
Sr
.No Ty
pe&Descr
ipt
ion
1
submi
t
Thi
scr
eat
esabut
tont
hataut
omat
ical
l
ysubmi
tsaf
orm.
NehaMi
dha,
Lect
urer GPPanchkul
a
2
r
eset
Thi
scr
eat
esabut
tont
hataut
omat
ical
l
yreset
sfor
m cont
rol
stot
hei
rini
ti
alv
alues.
3
but
ton
Thiscr
eat
esabuttont
hati
susedt
otr
iggeracl
i
ent
-si
descr
iptwhent
heuser
cl
ickst
hatbut
ton.
4
i
mage
Thi
screat
esacl
i
ckabl
ebut
tonbutwecanuseani
mageasbackgr
oundoft
he
but
ton.
Exampl
e
Her
eisexampl
eHTMLcodef
oraf
orm wi
tht
hreet
ypesofbut
tons−
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<tit
le>Fi
l
eUpl
oadBox</
ti
tl
e>
</head>
<body>
<form>
<i
nputt
ype="
submi t
"name="submi t
"value="Submit"/
>
<i
nputt
ype="
reset"name="r
eset"value="Reset"/>
<i
nputt
ype="
button"name="ok"val
ue=" OK"/>
<i
nputt
ype="
image"name=" i
magebut t
on"src=" /
html/
images/
logo.
png"/
>
</for
m>
</body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
Hi
ddenFor
m Cont
rol
s
Hi
ddenf or
m cont
rol
sareusedtohi
dedatai
nsi
dethepagewhi
chlateroncan
bepushedtotheserver
.Thi
scontr
olhi
desinsi
det
hecodeanddoesnotappearonthe
act
ualpage.For
NehaMi
dha,
Lect
urer GPPanchkul
a
example,f
oll
owinghiddenfor
mi sbeingusedt okeepcurrentpagenumber.Whena
userwil
lcl
icknextpagethent
hev al
ueofhiddencontrolwi
llbesentt
othewebserver
andtherei
twilldeci
dewhichpagewillbedi
splayednextbasedonthepassedcur
rent
page.
Exampl
e
Her
eisexampl
eHTMLcodet
oshowt
heusageofhi
ddencont
rol
−
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<tit
le>Fi
l
eUpl
oadBox</
ti
tl
e>
</head>
<body>
<form>
<p>Thi
sispage10</ p>
<i
nputtype="hidden"name="pagename"value=" 10"/>
<i
nputtype="submi t
"name="submi t
"val
ue=" Submit"/>
<i
nputtype="reset"name="
reset"value="Reset"/>
</form>
</body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
St
yleSheet
sinHTML
Cascadi
ngSt
yleSheet
s(CSS)descr
ibehowdocument
sar
epr
esent
edon
scr
eens,
inpr
int
,orper
hapshowt
heyar
epr
onounced.W3Chasact
ivel
ypr
omot
edt
he
useofst
ylesheet
sont
heWebsi
ncet
heconsor
ti
um wasf
oundedi
n1994.
Cascadi
ngSt
yleSheet
s(CSS)pr
ovi
deeasyandef
fect
iveal
ter
nat
ivest
ospeci
fy
v
ari
ousat
tri
but
esf
ort
heHTMLt
ags.Usi
ngCSS,
youcanspeci
fyanumberofst
yle
pr
oper
ti
esf
oragi
venHTMLel
ement
.Eachpr
oper
tyhasanameandav
alue,
separ
atedbyacol
on(
:)
.Eachpr
oper
tydecl
arat
ioni
ssepar
atedbyasemi
-col
on(
;)
.
Exampl
e
NehaMi
dha,
Lect
urer GPPanchkul
a
Fi
rstl
et'
sconsi
deranexampl
eofHTMLdocumentwhi
chmakesuseof<f
ont
>tagand
associ
atedat
tri
but
est
ospeci
fyt
extcol
orandf
ontsi
ze–
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>HTMLCSS</
ti
tl
e>
</head>
<body >
<p><f
ontcol
or="
green"si
ze="
5">Hel
l
o,Wor
ld!
</f
ont
></
p>
</body>
</
html
>
Wecanr
e-wr
it
eabov
eexampl
ewi
tht
hehel
pofSt
yleSheetasf
oll
ows−
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>HTMLCSS</
ti
tl
e>
</head>
<body>
<pst
yle="
col
or:
green;
font
-si
ze:
24px;
">Hel
l
o,Wor
ld!
</p>
</body>
</
html>
Thiswil
lpr
oducet
hef
oll
owi
ngr
esul
t–
Hel
l
o,Wor
ld!
Ty
peofCSS:
YoucanuseCSSi
nthr
eeway
siny
ourHTMLdocument−
Ext
ernalSt
yleSheet−Def
inest
ylesheetr
ulesi
nasepar
ate.
cssf
il
eandt
hen
i
ncl
udet
hatf
il
einy
ourHTMLdocumentusi
ngHTML<l
i
nk>t
ag.
I
nter
nalSt
yleSheet−Def
inest
ylesheetr
ulesi
nheadersect
ionoft
heHTML
documentusi
ng<st
yle>t
ag.
I
nli
neSt
yleSheet−Def
inest
ylesheetr
ulesdi
rect
lyal
ong-
wit
htheHTML
el
ement
susi
ngst
yleat
tri
but
e.
NehaMi
dha,
Lect
urer GPPanchkul
a
Ext
ernalSt
yleSheet
I
fy ou need to use yourstyl
e sheettov ari
ous pages,t
hen it
s always
recommendedt odefi
neacommonst ylesheeti
nasepar at
ef i
l
e.Acascadingstyl
e
sheetf
il
ewillhaveext
ensionas.cssanditwil
lbeincludedinHTMLfil
esusing<li
nk>
tag.
Exampl
e
Consi
derwedef
ineast
ylesheetf
il
est
yle.
csswhi
chhasf
oll
owi
ngr
ules−
.
red{
color:r
ed;
}
.
thick{
font-si
ze:
20px;
}
.
green
{color:
gre
en;
}
Herewedef i
nedthreeCSS ruleswhichwi l
lbeapplicabletothreediff
erent
classesdefi
nedf ortheHTMLt ags.Isuggestyoushouldnotbotherabouthowt hese
rulesarebeingdefi
nedbecauseyouwi l
ll
earnthem whi
lest
udyingCSS.Nowl et
'smake
useoft heaboveexternal
CSSfil
einourf ol
l
owingHTMLdocument–
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>HTMLExternalCSS</
ti
tl
e>
<l
i
nkr el
="st
ylesheet
"type="t
ext
/css"hr
ef="
/ht
ml/
sty
le.
css"
>
</head>
<body>
<pcl
ass="
red">Thisisred</p>
<pcl
ass="
thi
ck">Thisisthick</
p>
<pcl
ass="
green">Thisisgreen</p>
<pcl
ass="
thi
ckgr een">Thisist
hickandgr
een</
p>
</body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
Thi
sisr
ed
Thi
sis
t
hickThi
s
i
sgr
een
Thi
sist
hickandgr
een
NehaMi
dha,
Lect
urer GPPanchkul
a
I
nter
nalSt
yleSheet
Ifyouwanttoappl
yStyleSheetr
ulest
oasingl
edocumentonl
y,t
henyoucan
i
ncl
udethoserul
esinheadersect
ionoftheHTMLdocumentusi
ng<sty
le>t
ag.
Rul
esdef
inedi
nint
ernal
sty
lesheetov
err
idest
her
ulesdef
inedi
nanext
ernal
CSSf
il
e.Ex
ampl
e
Let
'
sre-
writ
eaboveexampl
eonceagai
n,buther
ewewi
l
lwr
it
est
ylesheetr
ulesi
nthe
sameHTMLdocumentusi
ng<sty
le>t
ag−
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<ti
tl
e>HTMLI
nter
nal
CSS</
ti
tl
e>
<styl
et y
pe=" text
/css"
>
.red{
color:r
ed;
}
.thi
ck{
font-
size:
20px;
}
.green
{color:
gre
en;
}
</styl
e>
</
head>
<body>
<pclass="
red">Thisisred</p>
<pclass="
thi
ck">Thisisthick</
p>
<pclass="
green">Thisisgreen</p>
<pclass="
thi
ckgr een">Thisist
hickandgr
een</
p>
</body
>
</
html>
Thiswi
llpr
oducet
hef
oll
owi
ngr
esul
t−
Thi
sisr
ed
Thi
sist
hick
NehaMi
dha,
Lect
urer GPPanchkul
a
Thi
sisgr
een
Thi
sist
hickandgr
een
I
nli
neSt
yleSheet
<head>
<t
it
le>HTMLI
nli
neCSS</
ti
tl
e>
</head>
<body>
<pst
yle="
color
:red;
">Thisisred</p>
<pst
yle="
font-
size:
20px;">Thi
si sthi
ck</p>
<pst
yle="
color
:green;
">Thisisgreen</p>
<pst
yle="
color
:green;
font-si
ze:20px;
">Thisi
sthi
ckandgr
een</
p>
</body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
Thi
sisr
ed
Thi
sis
t
hickThi
s
i
sgr
een
Thi
sist
hickandgr
een
NehaMi
dha,
Lect
urer GPPanchkul
a
Lay
out
sinHTML
Awebpagel
ayouti
sver
yimport
antt
ogiv
ebett
erl
ooktoyourwebsi
te.I
ttakes
consi
der
abl
etimetodesi
gnawebsi
te'
slay
outwit
hgr
eatl
ookandfeel
.
Now-a-
days,al
lmoder nwebsi t
esareusingCSSandJav aScr
iptbasedframeworkt o
comeupwi thr
esponsiveanddy nami
cwebsitesbutyoucancreateagoodl ay
outusi
ng
simpl
eHTMLt ablesordivi
siontagsincombinati
onwithotherformatti
ngtags.This
chapt
erwillgi
vey oufew examplesonhow tocreateasimplebutwor ki
ngl ay
outfor
yourwebpageusingpureHTMLandi tsatt
ri
butes.
HTMLLay
out-Usi
ngTabl
es
Thesimplestandmostpopularwayofcreat
ingl
ayout
sisusi
ngHTML<t abl
e>tag.
These t
ablesarearr
angedincolumnsandrows,soyoucanuti
l
izet
heserowsand
col
umnsinwhat ev
erwayyouli
ke.
Exampl
e
Forexampl
e,t
hefoll
owingHTMLl ayoutexampl
eisachi
evedusi
ngatabl
ewi
th3r
ows
and2columnsbuttheheaderandfootercol
umnspansbothcol
umnsusi
ngt
he
col
spanatt
ri
bute−
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<tit
le>HTMLLay
outusi
ngTabl
es</
ti
tl
e>
</head>
<body>
<tabl
ewi
dth="
100%"bor
der="
0">
<tr>
<t
dcolspan=" 2"bgcolor="#b5dcb3">
<h1>Thisi
sWebPageMai nti
tle</
h1>
</td>
</tr
>
<trval
ign="top">
<tdbgcolor="#aaa"wi dt
h=" 50">
<b>MainMenu</ b><br
/>HTML<br/ >
PHP<br
/>PERL..
.
</td>
<t
dbgcolor="#eee"widt
h="100"hei
ght=
"200"
>TechnicalandManager
ial
Tutor
ial
s
NehaMi
dha,
Lect
urer GPPanchkul
a
</td>
</
tr>
<t
r>
<tdcolspan="2"bgcol
or="
#b5dcb3"
>
<cent er>
Copy ri
ght©2007
</ center
>
</td>
</
tr>
</t
abl
e>
</
body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
Mul
ti
pleCol
umnsLay
out-Usi
ngTabl
es
Youcandesignyourwebpagetoputyourwebcontenti
nmulti
plepages.Youcankeep
yourcontenti
nmi ddl
ecolumnandy oucanusel eftcol
umnt ousemenuandr i
ght
columncanbeusedt oputadver
ti
sementorsomeot herst
uff
.Thislayoutwi
l
lbev er
y
simil
art
owhatwehav eatourwebsi
tetutor
ial
spoi
nt.
com.
Exampl
e
Her
eisanexampl
etocr
eat
ethr
eecol
umnl
ayout−
<!
DOCTYPEht
ml>
NehaMi
dha,
Lect
urer GPPanchkul
a
<ht
ml >
<head>
<tit
le>ThreeColumnHTMLLay out</t
it
le>
</head>
<body >
<tablewidth="100%"bor der="0">
<t
rval
ign="top">
<tdbgcolor="#aaa"wi dt
h=" 20%">
<b>MainMenu</ b><br
/>HTML<br/ >
PHP<br
/>PERL...
</td>
<t
dbgcolor="
#b5dcb3"hei
ght="200"widt
h=
"60%"
>Techni
calandManageri
alTutor
ial
s
</
td>
<tdbgcolor="
#aaa"widt
h="
20%"
>
<b>RightMenu</
b><br
/>HTML<br/ >
PHP<br
/>PERL. .
.
</td>
</
tr>
<tabl
e>
</
body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
NehaMi
dha,
Lect
urer GPPanchkul
a
HTMLLay
out
s-Usi
ngDI
V,SPAN
The<di
v>elementisabl ocklev
elel
ementusedf
orgr
oupi
ngHTMLelement
s.
Whil
et he<di
v>tagisabl ock-l
evelel
ement,t
heHTML<span>el
ementi
susedfor
gr
oupingel
ement
satani nl
inelevel
.
Althoughwecanachi
eveprett
ynicelay
outswi
thHTMLt
abl
es,buttabl
esweren'
t
reall
ydesi
gnedasalayoutt
ool.Tabl
esaremor
esuit
edt
opresent
ingtabul
ardat
a.
Note−ThisexamplemakesuseofCascadi
ngStyl
eSheet(CSS),sobef
ore
underst
andi
ngthi
sexampleyouneedtohaveabett
erunderst
andingonhowCSS
works.
Exampl
e
Her
ewewillt
ryt
oachi
evesamer
esultusing<di
v>t
agal
ongwi
thCSS,
what
every
ou
hav
eachi
evedusi
ng<t
able>t
agi
npreviousexampl
e.
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<tit
le>HTMLLay
out
susi
ngDI
V,SPAN</
ti
tl
e>
</head>
<body>
<divst
yle="
widt
h:100%"
>
<divsty
le="
backgr
ound-
col
or:
#b5dcb3;
widt
h:100%"
>
<h1>Thisi
sWebPageMaintit
le</
h1>
</div
>
<divstyl
e=" backgr
ound-
color
:#aaa;
hei
ght
:200px;
widt
h:100px;
float
:l
eft
;"
>
<div><b>Main
Menu</ b></div>HTML<br/>
PHP<br
/>PERL. .
.
</div
>
<divsty
le="
background-
col
or:
#eee;hei
ght:
200px;
widt
h:350px;
float
:l
eft
;">
<p>Techni
calandManageri
alTut
orial
s</
p>
</div
>
<divstyl
e=" backgr
ound-
color
:#aaa;
hei
ght
:200px;
widt
h:100px;
float
:r
ight
;"
>
<div><b>Right
Menu</ b></div>HTML<br/>
PHP<br
/>PERL. .
.
</div
>
NehaMi
dha,
Lect
urer GPPanchkul
a
<divsty
le="backgr
ound-
col
or:
#b5dcb3;
clear
:bot
h">
<center>
Copyri
ght©2007
</center
>
</div
>
</di
v>
</
body>
</
html
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t–
NehaMi
dha,
Lect
urer GPPanchkul
a
Chapt
er2
PHP
I
ntr
oduct
ion
PHPst
art
edoutasasmal
lopensour
cepr
ojectt
hatev
olv
edasmor
eand
mor
epeopl
efoundouthow usef
uli
twas.RasmusLer
dor
funl
eashedt
hef
ir
st
v
ersi
onofPHPwaybacki
n1994.
PHPi
sar
ecur
siv
eacr
ony
mfor"
PHP:
Hyper
textPr
epr
ocessor
".
PHPi
saser
versi
descr
ipt
ingl
anguaget
hati
sembeddedi
nHTML.I
tisusedt
o
manage dy
nami
c cont
ent
,dat
abases,sessi
on t
racki
ng,ev
en bui
l
d ent
ir
e e-
commer
cesi
tes.
I
tisi
ntegr
ated wi
th a number of popul
ar dat
abases,i
ncl
udi
ng My
SQL,
Post
greSQL,
Oracl
e,Sy
base,
Inf
ormi
x,andMi
crosof
tSQLSer
ver
.
PHPi
spl
easi
ngl
yzi
ppyi
nit
sexecut
ion,especi
all
ywhencompi
l
edasanApache
modul
eont
heUni
xsi
de.TheMy
SQLser
ver
,oncest
art
ed,execut
esev
env
ery
compl
exquer
ieswi
thhuger
esul
tset
sinr
ecor
d-set
ti
ngt
ime.
PHPsuppor
tsal
argenumberofmaj
orpr
otocol
ssuchasPOP3,
IMAP,
andLDAP.
PHP4addedsuppor
tforJav
aanddi
str
ibut
edobj
ectar
chi
tect
ures(
COM and
CORBA)
,maki
ngn-
ti
erdev
elopmentapossi
bil
i
tyf
ort
hef
ir
stt
ime.
PHPi
sfor
giv
ing:
PHPl
anguaget
ri
est
obeasf
orgi
vi
ngaspossi
ble.
PHPSy
ntaxi
sC-
Like.
CommonusesofPHP
PHPper
for
mssy
stem f
unct
ions,
i.
e.f
rom f
il
esonasy
stem i
tcancr
eat
e,
open,
read,
wri
te,
andcl
oset
hem.
PHPcanhandl
efor
ms,
i.
e.gat
herdat
afr
om f
il
es,
sav
edat
atoaf
il
e,t
hrough
emai
lyoucansenddat
a,r
etur
ndat
atot
heuser
.
Youadd,
del
ete,
modi
fyel
ement
swi
thi
nyourdat
abaset
hroughPHP.
Accesscooki
esv
ari
abl
esandsetcooki
es.
NehaMi
dha,
Lect
urer GPPanchkul
a
Usi
ngPHP,
youcanr
est
ri
ctuser
stoaccesssomepagesofy
ourwebsi
te.
I
tcanencr
yptdat
a.
Char
act
eri
sti
csofPHP
Fi
vei
mpor
tantchar
act
eri
sti
csmakePHP'
spr
act
ical
nat
urepossi
ble−
Si
mpl
i
cit
y
Ef
fi
ci
ency
Secur
it
y
Fl
exi
bil
i
ty
Fami
l
iar
it
y
HowPHPwor
ks&Basi
cPHPSy
ntax
PHPi
sser
versidescri
pti
nglanguage.So,
iti
sexecut
eont
heser
ver
,andt
he
pl
ainHTMLr
esul
tissentbacktothebrowser.
PHPFi
leSt
ruct
ure
APHPscr i
ptcanbepl
acedanywher
einthe
document.APHPscri
ptst
art
swith<?phpand
endswit
h? >
Sy
ntax:
<?
php/
/PHPcodegoesher
e?>
Thedef
aul
tfi
l
eext
ensi
onf
orPHPf
il
esi
s.php.
APHPf
il
enor
mal
l
ycont
ainsHTMLt
ags,
andsomePHPscr
ipt
ingcode.
Bel
ow,
wehav
eanexampl
eofasi
mpl
ePHPf
il
est
ruct
ure,
wit
haPHPscr
ipt
t
hatusesabui
l
t-i
nPHPf
unct
ion"
echo"t
oout
putt
het
ext"
Hel
l
oPHP!
"onawebpage:
<ht
ml>
<head>
<ti
tl
e>Hel
l
oPHP</
ti
tl
e>
</
head>
<body>
<?
phpecho"
Hel
l
o,PHP!
";
?>
</body>
NehaMi
dha,
Lect
urer GPPanchkul
a
</
html
>
I
twi
l
lpr
oducef
oll
owi
ngr
esul
t−
Hel
l
o,PHP!
Thephp.
iniFi
le
ThePHPconf
igur
ati
onf
il
e,php.
ini
,i
sthef
inalandmosti
mmedi
atewayt
oaf
fect
PHP'
sfunct
ional
i
ty.Thephp.
inif
il
eisr
eadeacht
imePHPi
sini
ti
ali
zed.I
not
herwor
ds,
whenev
erht
tpdi
srest
art
edf
ort
hemodul
ever
sionorwi
theachscr
iptexecut
ionf
ort
he
CGIv
ersi
on.I
fyourchangei
sn’
tshowi
ngup,r
emembert
ost
opandr
est
artht
tpd.I
fit
st
il
li
sn’
tshowi
ngup,
usephpi
nfo(
)tocheckt
hepat
htophp.
ini
.
Theconf
igur
ati
onf
il
eiswel
lcomment
edandt
hor
ough.Key
sar
ecasesensi
ti
ve,
key
wor
dval
uesar
enot
;whi
tespace,andl
i
nesbegi
nni
ngwi
thsemi
col
onsar
eignor
ed.
Bool
eanscanber
epr
esent
edby1/
0,Yes/
No,On/
Off
,orTr
ue/
Fal
se.Thedef
aul
tval
ues
i
nphp.
ini
-di
stwi
l
lresul
tinar
easonabl
ePHPi
nst
all
ati
ont
hatcanbet
weakedl
ater
.
Her
ewear
eexpl
aini
ngt
hei
mpor
tantset
ti
ngsi
nphp.
iniwhi
chy
oumayneed
f
ory
ourPHPPar
ser
.
shor
t_open_
tag=Of
f
Shor
topent
agsl
ookl
i
ket
his:
<??
>.Thi
sopt
ionmustbesett
oOf
fify
ouwantt
o
useXMLf
unct
ions.
saf
e_mode=Of
f
I
fthi
sissett
oOn,
youpr
obabl
ycompi
l
edPHPwi
tht
he-
-enabl
e-saf
e-modef
lag.Saf
e
modei
smostr
elev
antt
oCGIuse.Seet
heexpl
anat
ioni
nthesect
ion"
CGIcompi
l
e-
t
imeopt
ions"
.saf
e_mode_
exec_
dir=[
DIR]
Thi
sopt
ioni
srel
evantonl
yifsaf
emodei
son;
itcanal
sobesetwi
tht
he-
-wi
th-
exec-
di
rfl
agdur
ingt
heUni
xbui
l
dpr
ocess.PHPi
nsaf
emodeonl
yexecut
esext
ernal
bi
nar
iesoutoft
hisdi
rect
ory
.Thedef
aul
tis/
usr
/l
ocal
/bi
n.Thi
shasnot
hingt
odo
wi
thser
vingupanor
mal
PHP/
HTMLWebpage.
saf
e_mode_
all
owed_
env
_var
s=[
PHP_
]
Thi
sopt
ionset
swhi
chenv
ironmentv
ari
abl
esuser
scanchangei
nsaf
emode.The
def
aul
tisonl
ythosev
ari
abl
espr
ependedwi
th"
PHP_
".I
fthi
sdi
rect
ivei
sempt
y,most
v
ari
abl
esar
eal
ter
abl
e.saf
e_mode_
prot
ect
ed_
env
_var
s=[
LD_
LIBRARY_
PATH]
NehaMi
dha,
Lect
urer GPPanchkul
a
Thi
sopt
ionset
swhi
chenv
ironmentv
ari
abl
esuser
scan'
tchangei
nsaf
emode,
eveni
f
saf
e_mode_
all
owed_
env
_var
sissetper
missi
vel
y
di
sabl
e_f
unct
ions=[
funct
ion1,
funct
ion2.
..
]
Awel
comeaddi
ti
ont
oPHP4conf
igur
ati
onandoneper
pet
uat
edi
nPHP5i
stheabi
l
ityt
o
di
sabl
esel
ect
edf
unct
ionsf
orsecur
it
yreasons.Pr
evi
ousl
y,t
hisnecessi
tat
edhand-
edi
ti
ngt
heCcodef
rom whi
chPHPwasmade.Fi
l
esy
stem,
syst
em,
andnet
wor
k
f
unct
ionsshoul
dpr
obabl
ybet
hef
ir
stt
ogobecauseal
l
owi
ngt
hecapabi
l
ityt
owr
it
e
f
il
esandal
tert
hesy
stem ov
erHTTPi
snev
ersuchasaf
eidea.
max_
execut
ion_
time=30
Thef
unct
ionset
_ti
me_
li
mit
()won.
twor
kinsaf
emode,
sot
hisi
sthemai
nwayt
omake
ascr
iptt
imeouti
nsaf
emode.I
nWi
ndows,
youhav
etoabor
tbasedonmaxi
mum
memor
yconsumedr
athert
hant
ime.Youcanal
souset
heApachet
imeoutset
ti
ngt
o
t
imeouti
fyouuseApache,
butt
hatwi
l
lappl
ytonon-
PHPf
il
esont
hesi
tet
oo.
PHPVar
iabl
es
Themai
nwayt
ost
orei
nfor
mat
ioni
nthemi
ddl
eofaPHPpr
ogr
am i
sbyusi
nga
v
ari
abl
e.Her
ear
ethemosti
mpor
tantt
hingst
oknowaboutv
ari
abl
esi
nPHP.
Al
lvar
iabl
esi
nPHPar
edenot
edwi
thal
eadi
ngdol
l
arsi
gn(
$).
Thev
alueofav
ari
abl
eist
hev
alueofi
tsmostr
ecentassi
gnment
.
Var
iabl
esar
eassi
gnedwi
tht
he=oper
ator
,wi
tht
hev
ari
abl
eont
hel
eft
-handsi
de
andt
heexpr
essi
ont
obeev
aluat
edont
her
ight
.
Var
iabl
escan,
butdonotneed,
tobedecl
aredbef
oreassi
gnment
.
Var
iabl
esi
nPHPdonothav
eint
ri
nsi
cty
pes-av
ari
abl
edoesnotknowi
n
adv
ancewhet
heri
twi
l
lbeusedt
ost
oreanumberorast
ri
ngofchar
act
ers.
Var
iabl
esusedbef
oret
heyar
eassi
gnedhav
edef
aul
tval
ues.
PHPdoesagoodj
obofaut
omat
ical
l
yconv
ert
ingt
ypesf
rom onet
oanot
her
whennecessar
y.
NehaMi
dha,
Lect
urer GPPanchkul
a
PHPv
ari
abl
esar
ePer
l-
li
ke.
PHPhasat
otal
ofei
ghtdat
aty
peswhi
chweuset
oconst
ructourv
ari
abl
es−
I
nteger
s−ar
ewhol
enumber
s,wi
thoutadeci
mal
poi
nt,
li
ke4195.
Doubl
es−ar
efl
oat
ing-
poi
ntnumber
s,l
i
ke3.
14159or49.
1.
Bool
eans−hav
eonl
ytwopossi
blev
aluesei
thert
rueorf
alse.
NULL−i
saspeci
alt
ypet
hatonl
yhasonev
alue:
NULL.
St
ri
ngs−ar
esequencesofchar
act
ers,
li
ke'
PHPsuppor
tsst
ri
ngoper
ati
ons.
'
Ar
ray
s−ar
enamedandi
ndexedcol
l
ect
ionsofot
herv
alues.
Obj
ect
s−ar
einst
ancesofpr
ogr
ammer
-def
inedcl
asses,
whi
chcanpackageup
bot
hot
herki
ndsofv
aluesandf
unct
ionst
hatar
especi
fi
ctot
hecl
ass.
Resour
ces−ar
especi
alv
ari
abl
est
hathol
dref
erencest
oresour
cesext
ernal
to
PHP(
suchasdat
abaseconnect
ions)
.
I
nteger
s
Theyarewholenumbers,wit
houtadeci
malpoint
,li
ke4195.Theyar
ethesimplest
type.Theycorr
espondtosimplewholenumber
s,bothposi
ti
veandnegati
ve.I
ntegers
canbeassignedtovar
iabl
es,ort
heycanbeusedinexpr
essi
ons,l
ikeso−
$i
nt_v
ar=12345;
$anot
her
_int=-
12345+12345;
I
ntegercanbeindeci
mal(base10),octal(
base8)
,andhexadeci
mal(base16)
for
mat .Decimalf
ormati
sthedef
ault,
octali
nteger
sarespeci
fi
edwit
hal eadi
ng0,and
hexadecimalshaveal
eadi
ng0x.
Formostcommonplatf
orms, t
helar
gestint
egeri
s(2**31.1)(
or2,
147,
483,
647)
,
andt
hesmallest(
mostnegat
ive)i
ntegeri
s.(2**
31.1)(or.
2,147,
483,
647)
.
Doubl
es
Theyli
ke3.14159or49.1.Bydefaul
t,doubl
espr
intwi
tht
hemi
nimum numberof
deci
mal pl
acesneeded.Forexample,
thecode−
<?
php
$many=2.
2888800;
$many_
2=2.2111200;
$few=$many+$many _
2;
NehaMi
dha,
Lect
urer GPPanchkul
a
pr
int
("
$many+$many
_2=$f
ew<br
>")
;
?
>
I
tpr
oducest
hef
oll
owi
ngbr
owser
out
put−2.
28888+2.
21112=4.
5
Bool
ean
Theyhaveonl
ytwopossibl
evalueseit
hert
rueorf
alse.PHPprovi
desacoupl
e
ofconst
antsespeci
all
yforuseasBooleans:
TRUEandFALSE, whi
chcanbeusedli
ke
so−
i
f(TRUE)
pri
nt(
"Thi
swi
l
lal
way
spr
int
<br
>")
;
el
se
pri
nt(
"Thi
swi
l
lnev
erpr
int
<br
>")
;
I
nter
pret
ingot
hert
ypesasBool
eans
Her
ear
ether
ulesf
ordet
ermi
net
he"
tr
uth"ofanyv
aluenotal
readyoft
heBool
eant
ype−
I
fthev
aluei
sanumber
,iti
sfal
sei
fex
act
lyequal
tozer
oandt
rueot
her
wise.
I
fthev
aluei
sast
ri
ng,
iti
sfal
sei
fthest
ri
ngi
sempt
y(haszer
ochar
act
ers)ori
s
t
hest
ri
ng"
0",
andi
str
ueot
her
wise.
Val
uesoft
ypeNULLar
eal
way
sfal
se.
I
fthev
aluei
sanar
ray
,iti
sfal
sei
fitcont
ainsnoot
herv
alues,andi
tist
rue
otherwi
se.Foranobject
,contai
ningav
aluemeanshav
ingamemberv
ari
abl
e
thathasbeenassi
gnedav al
ue.
Val
idr
esour
cesar
etr
ue(
alt
houghsomef
unct
ionst
hatr
etur
nresour
ceswhen
t
heyar
esuccessf
ulwi
l
lret
urnFALSEwhenunsuccessf
ul)
.
Don'
tusedoubl
easBool
eans.
Eachoft
hef
oll
owi
ngvari
ableshast
het
rut
hval
ueembeddedi
nit
snamewheni
tis
usedi
naBool
eancont
ext.
$t
rue_num =3+0. 14159;
$t
rue_str=" Tri
edandtrue"
$t
rue_array[
49]=" Anarrayel
ement
";
$f
alse_array=ar r
ay(
);
$f
alse_null=NULL;
$f
alse_num =999-999;
$f
alse_str=" "
;
NehaMi
dha,
Lect
urer GPPanchkul
a
NULL
NULLi
saspeci
alt
ypet
hatonl
yhasonev
alue:
NULL.Togi
veav
ari
abl
ethe
NULLv
alue,
simpl
yassi
gni
tli
ket
his−
$my
_var=NULL;
Thespeci
alconst
antNULLi
scapi
tal
i
zedbyconv
ent
ion,
butact
ual
l
yiti
scase
i
nsensi
ti
ve;
youcoul
djustaswel
lhav
ety
ped−
$my
_var=nul
l
;
Av
ari
abl
ethathasbeenassi
gnedNULLhast
hef
oll
owi
ngpr
oper
ti
es−
I
tev
aluat
est
oFALSEi
naBool
eancont
ext
.
I
tret
urnsFALSEwhent
est
edwi
thI
sSet
()f
unct
ion.
St
ri
ngs
Theyar
esequencesofchar
act
ers,
li
ke"
PHPsuppor
tsst
ri
ngoper
ati
ons"
.
Fol
l
owi
ngar
eval
i
dexampl
esofst
ri
ng
$st
ri
ng_
1="
Thi
sisast
ri
ngi
ndoubl
equot
es"
;
$st
ri
ng_
2='
Thi
sisasomewhatl
onger
,si
ngl
yquot
edst
ri
ng'
;
$st
ri
ng_
39="
Thi
sst
ri
nghast
hir
ty-
ninechar
act
ers"
;
$st
ri
ng_
0="
";/
/ast
ri
ngwi
thzer
ochar
act
ers
Si
ngl
yquot
edst
ri
ngsar
etr
eat
edal
mostl
i
ter
all
y,wher
easdoubl
yquot
edst
ri
ngs
r
epl
acev
ari
abl
eswi
tht
hei
rval
uesaswel
lasspeci
all
yint
erpr
eti
ngcer
tai
nchar
act
er
sequences.
<?
php
$v
ari
abl
e="
name"
;
$l
i
ter
all
y='
My$v
ari
abl
ewi
l
lnotpr
int
!'
;
pr
int
($l
i
ter
all
y);
NehaMi
dha,
Lect
urer GPPanchkul
a
pr
int"
<br
>";
$l
i
ter
all
y="
My$v
ari
abl
ewi
l
l
pr
int
!"
;pr
int
($l
i
ter
all
y);
?
>
Thi
swi
l
lpr
oducef
oll
owi
ngr
esul
t−
My$v
ari
abl
ewi
l
lnot
pr
int
!Mynamewi
l
lpr
int
Ther
ear
enoar
ti
fi
cial
li
mit
sonst
ri
ngl
engt
h-wi
thi
ntheboundsofav
ail
abl
ememor
y,
y
ououghtt
obeabl
etomakear
bit
rar
il
ylongst
ri
ngs.
St
ri
ngst
hatar
edel
i
mit
edbydoubl
equot
es(
asi
n"t
his"
)ar
epr
epr
ocessedi
nbot
hthe
f
oll
owi
ngt
woway
sbyPHP−
Cer
tai
nchar
act
ersequencesbegi
nni
ngwi
thbacksl
ash(
\)ar
erepl
acedwi
th
speci
alchar
act
ers
Var
iabl
enames(
star
ti
ngwi
th$)ar
erepl
acedwi
thst
ri
ngr
epr
esent
ati
onsoft
hei
rval
ues.
Theescape-
sequencer
epl
acement
sar
e−
\
nisr
epl
acedbyt
henewl
i
nechar
act
er
\
risr
epl
acedbyt
hecar
ri
age-
ret
urnchar
act
er
\
tisr
epl
acedbyt
het
abchar
act
er
\
$isr
epl
acedbyt
hedol
l
arsi
gni
tsel
f($)
\
"isr
epl
acedbyasi
ngl
edoubl
e-quot
e("
)
\
\isr
epl
acedbyasi
ngl
ebacksl
ash(
\)
Var
iabl
eScope
Scopecanbedef
inedast
her
angeofav
ail
abi
l
ityav
ari
abl
ehast
othepr
ogr
am i
n
whi
chi
tisdecl
ared.PHPv
ari
abl
escanbeoneoff
ourscopet
ypes−
Local
var
iabl
es
NehaMi
dha,
Lect
urer GPPanchkul
a
Funct
ionpar
amet
ers
Gl
obal
var
iabl
es
St
ati
cvar
iabl
es
LocalVar
iabl
es
Av
ari
abl
edecl
aredi
naf
unct
ioni
sconsi
der
edl
ocal
;thati
s,i
tcanber
efer
enced
sol
elyi
nthatf
unct
ion.Anyassi
gnmentout
sideoft
hatf
unct
ionwi
l
lbeconsi
der
edt
obe
anent
ir
elydi
ff
erentv
ari
abl
efr
om t
heonecont
ainedi
nthef
unct
ion−
<?
php
$x=4;
f
uncti
onassignx(){
$x=0;
pr
int"\
$xinsidefunct
ioni
s$x.<br/
>";
}
assi
gnx()
;
pri
nt"\
$xout
sideoff
unct
ioni
s$x.<br/
>";
?
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
$xi
nsi
def unct
ioni
s0.
$xout
sideoffunct
ioni
s4.
Funct
ionPar
amet
ers
Functi
onparamet
ersaredecl
aredaf
terthefunct
ionnameandi
nsi
de
par
ent
heses.Theyar
edeclar
edmuchl i
keatypical
vari
ablewoul
dbe−
<?php
//mul tipl
yav alueby10andr etur
nitt
othe
callerfuncti
onmul t
ipl
y($val
ue){
$value=$v alue*
10;return$value;
}
$retval=mul ti
ply(10);
Print"Returnvalueis$retval
\n"
;
?>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
NehaMi
dha,
Lect
urer GPPanchkul
a
Ret
urnv
aluei
s100
Gl
obalVar
iabl
es
I
ncont
rastt
olocalv
ari
abl
es,
agl
obalv
ari
abl
ecanbeaccessedi
nanypar
toft
he
pr
ogr
am.Howev
er,
inor
dert
obemodi
fi
ed,
agl
obalv
ari
abl
emustbeexpl
i
cit
lydecl
ared
t
o begl
obali
nthef
unct
ioni
nwhi
chi
tist
o bemodi
fi
ed.Thi
sisaccompl
i
shed,
conv
eni
ent
lyenough,bypl
aci
ngt
hekey
wor
dGLOBALi
nfr
ontoft
hev
ari
abl
ethat
shoul
dber
ecogni
zedasgl
obal
.Pl
aci
ngt
hiskey
wor
dinf
rontofanal
readyex
ist
ing
v
ari
abl
etel
l
sPHPt
ouset
hev
ari
abl
ehav
ingt
hatname.Consi
deranexampl
e−
<?
php
$somev
ar=15;
f
uncti
onaddi
t(
)
{GLOBAL
$somevar
;
$somevar
++;
pr
int"
Somev
ari
s$somev
ar"
;
}
addi
t(
);
?
>
Thiswi
llpr
oducet
hef
oll
owi
ng
r
esul
t−Somev
ari
s16
St
ati
cVar
iabl
es
Thef
inalt
ypeofv
ari
abl
escopi
ngt
hatIdi
scussi
sknownasst
ati
c.I
ncont
rastt
o
t
hev
ari
abl
esdecl
aredasf
unct
ionpar
amet
ers,whi
char
edest
roy
edont
hef
unct
ion'
s
exi
t,ast
ati
cvar
iabl
ewi
l
lnotl
osei
tsv
aluewhent
hef
unct
ionexi
tsandwi
l
lst
il
lhol
d
t
hatv
alueshoul
dthef
unct
ionbecal
l
edagai
n.
Youcandecl
areav
ari
abl
etobest
ati
csi
mpl
ybypl
aci
ngt
hekey
wor
dSTATI
Cinf
rontof
t
hev
ari
abl
ename.
<?
php
functi
onkeep_
track(
)
{STATI
C$count=
0;
NehaMi
dha,
Lect
urer GPPanchkul
a
$count++;
pri
nt$count;
pri
nt"<br/
>";
}
keep_
track(
);
keep_
track(
);
keep_
track(
Thiswi
llproducet
hef
oll
owi
ngr
esul
t−
1
2
3
PHPCondi
ti
onalSt
atement
s
Li
kemostpr
ogr
ammi
ngl
anguages,PHP al
soal
l
owsy
out
owr
it
ecodet
hat
per
for
m di
ff
erentact
ions based on t
he r
esul
ts ofa l
ogi
calorcompar
ati
vet
est
condi
ti
onsatr
unt
ime.Thi
smeans,y
ou can cr
eat
etestcondi
ti
ons i
nthe f
orm of
expr
essi
ons t
hat ev
aluat
es t
oei
thert
rueorf
alseandbasedont
heser
esul
tsy
oucan
per
for
m cer
tai
nact
ions.
Ther
ear
esev
eral
stat
ement
sinPHPt
haty
oucanuset
omakedeci
sions:
Thei
fst
atement
Thei
f.
..
elsest
atement
Thei
f.
..
elsei
fel
sest
atement
Theswi
tchcasest
atement
Wewi
l
lexpl
oreeachoft
hesest
atement
sint
hecomi
ngsect
ions.
Thei
fSt
atement
Thei
fst
atementi
susedt
oexecut
eabl
ockofcodeonl
yift
hespeci
fi
edcondi
ti
on
ev
aluat
est
otr
ue.Thi
sist
hesi
mpl
estPHP'
scondi
ti
onal
stat
ement
sandcanbe
wr
it
tenl
i
ke:
i
f(
condi
ti
on){
/
/Codetobeexecut
ed
}
Thef
oll
owi
ngexampl
ewi
l
lout
put"
Hav
eani
ceweekend!
"ift
hecur
rentdayi
sFr
iday
:
NehaMi
dha,
Lect
urer GPPanchkul
a
Example
<?php
$d=date("
D");
i
f($d=="Fr
i"
){
echo"
Hav eani
ceweekend!
";
}
?>
Thei
f.
..
elseSt
atement
You can enhancet
hedeci
sion maki
ng pr
ocessbypr
ovi
ding an al
ter
nat
ivechoi
ce
t
hr nganel
oughaddi sest
atementt
othei
fst
at .Thei
ement f.
..
elsest
atemental
l
owsy
ou
t
oexecut
eonebl
ockofcodei
fthespeci
fi
edcondi
ti
oni
sev
aluat
est
otr
ueandanot
her
bl
ockofcodei
fiti
sev
aluat
est
ofal
se.I
tcanbewr
it
ten,
li
ket
his:
i
f(condi
ti
on){
//Codetobeexecut
edi
fcondi
ti
oni
strue
}else{
//Codetobeexecut
edi
fcondi
ti
oni
sfalse
}
Thefoll
owi
ngexamplewi
ll
out
put"
Haveani
ceweekend!
"ift
hecur
rentdayi
sFr
iday
,
other
wisei
twi
llout
put"
Haveani
ceday
!"
Example
<?php
$d=dat e("
D");
i
f($d==" Fr
i"
){
echo"Hav eani
ceweekend!
";
}else{
echo"Hav eani
ceday!
";
}
?>
Thei
f.
..
elsei
f.
..
elseSt
atement
Thei
f.
..
elsei
f.
..
elseaspeci
alst
atementt
hati
susedt
ocombi
nemul
ti
plei
f.
..
elsest
atement
s.
i
f(condi
ti
on1)
{
//Codetobeexecut
edi
fcondi
ti
on1istr
ue
}elsei
f(
condi
ti
on2)
{
//Codetobeexecut
edi
fthecondi
ti
on1isf
alseandcondi
ti
on2i
str
ue
}else{
NehaMi
dha,
Lect
urer GPPanchkul
a
/
/Codet
obeexecut
edi
fbot
hcondi
ti
on1andcondi
ti
on2ar
efal
se
}
Thef
oll
owingexampl
ewill
output"
Haveaniceweekend!
"ift
hecurr
entdayisFri
day,
and"
HaveaniceSunday
!"i
fthecur
rentdayi
sSunday,ot
herwi
seitwil
lout
put"Havea
ni
ceday!
"
Example
<?php
$d=dat e("
D");
i
f($d==" Fr
i"
){
echo"Hav eaniceweekend!
";
}elsei
f(
$d==" Sun"){
echo"Hav eaniceSunday!
";
}else{
echo"Hav eaniceday!
";
}
?>
TheTer
nar
yOper
ator
Thet
ernar
yoper
atorpr
ovi
desashor
thandwayofwr
it
i hei
ngt f.
..
elsest
atement
s.
Thet
ernar
yoper
atori
srepr
esent
edbyt
hequest
ionmar
k(?
)sy
mbolandi
ttakest
hree
oper
ands:
acondi
ti
ont
ocheck,
aresul
tfort
rue,
andar
esul
tforf
alse.
Tounder
standhowt
hisoper
atorwor
ks,
consi
dert
hef
oll
owi
ngexampl
es:
Exampl
e
<?
php
i
f(
$age<18)
{
echo'
Chi
l
d';
//Di
spl
ayChi
l
difagei
slesst
han18
}el
se{
echo'
Adul
t'
;//Di
spl
ayAdul
tifagei
sgr
eat
ert
hanorequal
to18
}
?
>
Usi
ngt
het
ernar
yoper
atort
hesamecodecoul
dbewr
it
teni
namor
ecompactway
:
Exampl
e
<?
phpecho(
$age<18)?'
Chi
l
d':
'Adul
t'
;?>
NehaMi
dha,
Lect
urer GPPanchkul
a
Thet
ernar
yoper
atori
ntheexampl
eabov
esel
ect
sthev
alueont
hel
eftoft
he
col
on(
i.
e.'
Chi
l
d')i
fthecondi
ti
onev
aluat
est
otr
ue(
i.
e.i
f$agei
slesst
han18)
,and
sel
ect
sthev
alueont
her
ightoft
hecol
on(
i.
e.'
Adul
t'
)ift
hecondi
ti
onev
aluat
est
ofal
se.
NehaMi
dha,
Lect
urer GPPanchkul
a
Oper
ator
sinPHP
Simpleanswercanbegiv
enusingexpr on4+5i
essi sequalto9.Her
e4and5are
cal
ledoperandsand+iscal
ledoper
ator
.PHPl anguagesuppor
tsfol
l
owingt
ypeof
operat
ors.
Ar
it
hmet
icOper
ator
s
Compar
isonOper
ator
s
Logi
cal
(orRel
ati
onal
)Oper
ator
s
Assi
gnmentOper
ator
s
Condi
ti
onal
(ort
ernar
y)Oper
ator
s
Let
shav
eal
ookonal
loper
ator
sonebyone.
Ar
it
hmet
icOper
ator
s
Ther
ear
efol
l
owi
ngar
it
hmet
icoper
ator
ssuppor
tedbyPHP
l
anguage−Assumev
ari
abl
eAhol
ds10andv
ari
abl
eBhol
ds20
t
hen−
ShowExampl
es
Oper
ator Descr
ipt
ion Exampl
e
+ Addst
wooper
ands A+Bwi
l
lgi
ve30
- Subt
ract
ssecondoper
andf
rom t
hef
ir
st A-Bwi
l
lgi
ve-
10
* Mul
ti
plybot
hoper
ands A*Bwi
l
lgi
ve200
/ Di
vi
denumer
atorbyde-
numer
ator B/Awi
l
lgi
ve2
% ModulusOper
atorandr
emai
nderofaf
terani
nteger B%Awi
l
lgi
ve0
di
vi
sion
++ I
ncr
ementoper
ator
,incr
easesi
ntegerv
aluebyone A++wi
l
lgi
ve11
-
- Decr
ementoper
ator
,decr
easesi
ntegerv
aluebyone A-
-wi
l
lgi
ve9
Compar
isonOper
ator
s
NehaMi
dha,
Lect
urer GPPanchkul
a
Ther
ear
efol
l
owi
ngcompar
isonoper
ator
ssuppor
tedbyPHP
l
anguageAssumev
ari
abl
eAhol
ds10andv
ari
abl
eBhol
ds20t
hen
−
ShowExampl
es
Oper
ator Descr
ipt
ion Exampl
e
== Checksift
heval
ueoftwooper
andsar
eequal
ornot
,ify
est
hen (
A==B)i
snot
condi
ti
onbecomestr
ue. t
rue.
!
= Checksi
fthev
alueoft
wooperandsar
eequalornot
,ifv
alues (
A! =B)i
s
ar
enotequalt
hencondi
ti
onbecomestrue. t
rue.
> Checksiftheval
ueoflef
toper
andisgreat
ert
hanthev
alueof (
A>B)isnot
ri
ghtoperand,
ifyest
hencondi
tionbecomest
rue. t
rue.
< Checksiftheval
ueofleftoper
andislessthant
hev
alueofr
ight (
A<B)i
str
ue.
operand,i
fyesthencondit
ionbecomestrue.
>= Checksi
ftheval
ueofl
eftoperandi
sgreat
ert
hanorequal
tot
he (
A>=B)i
snot
val
ueofri
ghtoper
and,
ifyesthencondi
ti
onbecomestr
ue. t
rue.
<= Checksi
ftheval
ueofl
eftoperandi
slessthanorequalt
othe (
A<=B)i
s
val
ueofri
ghtoper
and,
ifyesthencondi
ti
onbecomest r
ue. t
rue.
Logi
calOper
ator
s
Ther
ear
efol
l
owi
ngl
ogi
cal
oper
ator
ssuppor
tedbyPHP
l
anguageAssumev
ari
abl
eAhol
ds10andv
ari
abl
eBhol
ds
20t
hen−ShowExampl
es
Oper
ator Descr
ipt
ion Exampl
e
and Call
edLogicalANDoperat
or.I
fbot
htheoper
andsar
etr
ue (
AandB)
thencondi
tionbecomestr
ue. i
str
ue.
NehaMi
dha,
Lect
urer GPPanchkul
a
or Cal
ledLogi
calOROper at
or.I
fanyoft
het
wooper
andsar
enon (
AorB)i
s
zer
othencondit
ionbecomest r
ue. t
rue.
&& Cal
ledLogi
calANDoper at
or.I
fbot
htheoper
andsar
enon (
A&&B)i
s
zer
othencondit
ionbecomestrue. t
rue.
|
| Cal
ledLogi
calOROper at
or.I
fanyoft
het
wooper
andsar
enon (
A||B)i
s
zer
othencondit
ionbecomest r
ue. t
rue.
! Cal
ledLogicalNOTOperator
.Usetorever
sesthel
ogi
calstate !
(A&&B)i
s
ofi
tsoperand.Ifacondi
ti
onist
ruethenLogi
calNOToperator f
alse.
wil
lmakefalse.
Assi
gnmentOper
ator
s
Ther
ear
efol
l
owi
ngassi
gnmentoper
ator
ssuppor
tedbyPHP
l
anguage−ShowEx
ampl
es
Oper
ator Descr
ipt
ion Exampl
e
= Si
mpleassignmentoper
ator
,Assi
gnsv
aluesf
rom r
ight C=A+Bwi l
lassi
gn
si
deoperandstolef
tsi
deoperand val
ueofA+BintoC
+= AddANDassi gnmentoperator
,Itaddsri
ghtoper
and C+=Ai
sequi
val
ent
tothelef
toperandandassigntheresul
ttolef
t t
oC=C+A
operand
-
= Subtr
actANDassi gnmentoper
ator,
Itsubt
racts C-
=Aisequi
val
entt
oC
ri
ghtoper
andf r
om thelef
toperandandassignthe =C-A
resul
ttol
eftoperand
*
= Multi
plyANDassignmentoper
ator,
Itmul
tipli
es C*=Ai
sequi
val
ent
r
ightoperandwit
hthelef
toperandandassignthe t
oC=C*A
r
esulttolef
toper
and
NehaMi
dha,
Lect
urer GPPanchkul
a
/
= Div
ideANDassi gnmentoperat
or,
Itdi
videsleftoperand C/=Ai
sequi
val
entt
oC
wit
ht heri
ghtoperandandassi
gntheresulttoleft =C/A
operand
%= ModulusANDassi
gnmentoperator
,Itt
akesmodul
us C%=Ai
sequi
val
ent
usi
ngtwooper
andsandassigntheresul
ttol
eft t
oC=C%A
oper
and
Condi
ti
onalOper
ator
Oper
ator Descr
ipt
ion Exampl
e
?: Condi
ti
onal
Expr
essi
on I
fCondi
ti
oni
str
ue?Thenv
alueX:
Other
wisev
alueY
Oper
ator
sCat
egor
ies
Al
ltheoper
ator
swehav
edi
scussedabov
ecanbecat
egor
isedi
ntof
oll
owi
ngcat
egor
ies−
Unar
ypr
efi
xoper
ator
s,whi
chpr
ecedeasi
ngl
eoper
and.
Bi
nar
yoper
ator
s,whi
cht
aket
wooper
andsandper
for
m av
ari
etyofar
it
hmet
ic
andl
ogi
cal
oper
ati
ons.
Thecondi
ti
onal
oper
ator(
ater
nar
yoper
ator
),whi
cht
akest
hreeoper
andsand
ev
aluat
esei
thert
hesecondort
hir
dexpr
essi
on,
dependi
ngont
heev
aluat
ionof
t
hef
ir
stexpr
essi
on.
Assi
gnmentoper
ator
s,whi
chassi
gnav
aluet
oav
ari
abl
e.
Pr
ecedenceofPHPOper
ator
s
Oper
atorpr
ecedencedet
ermi
nest
hegr
oupi
ngoft
ermsi
nanexpr
essi
on.Thi
saf
fect
s
howanexpr
essi
oni
sev
aluat
ed.Cer
tai
noper
ator
shav
ehi
gherpr
ecedencet
hanot
her
s;
f
orexampl
e,t
hemul
ti
pli
cat
ion oper
atorhashi
gherpr
ecedencet
han t
headdi
ti
on
oper
ator−
NehaMi
dha,
Lect
urer GPPanchkul
a
Forexampl
ex=7+3*2;
Her
exi
sassi
gned13,
not20becauseoper
ator*hashi
gher
pr
ecedencet
han+soi
tfi
rstgetmul
ti
pli
edwi
th3*
2andt
henaddsi
nto7.
Hereoperator
swit
hthehighestpr
ecedenceappearatt
het opoft
hetable,
thosewit
h
t
helowestappearatthebott
om.Wi t
hinanexpr
ession,
higherpr
ecedenceoperat
ors
wil
lbeevaluat
edfi
rst
.
Cat
egor
y Oper
ator Associ
ati
vi
t
y
Unar
y !++-
- Ri
ghtt
olef
t
Mul
ti
pli
cat
iv */% Lef
ttor
ight
e
Addi
ti
ve +- Lef
ttor
ight
Rel
ati
onal <<=>>= Lef
ttor
ight
Equal
i
ty ==!
= Lef
ttor
ight
Logi
cal
AND && Lef
ttor
ight
Logi
cal
OR |
| Lef
ttor
ight
Condi
ti
onal ?
: Ri
ghtt
olef
t
Assi
gnment =+=-
=*=/
=%= Ri
ghtt
olef
t
NehaMi
dha,
Lect
urer GPPanchkul
a
Deci
sionMaki
ng
Thei
f,el
sei
f..
.el
seandswi
tchst
atement
sar
eusedt
otakedeci
sionbasedont
he
di
ff
erentcondi
ti
on.
Youcanusecondi
ti
onal
stat
ement
siny
ourcodet
omakey
ourdeci
sions.PHP
suppor
tsf
oll
owi
ngt
hreedeci
sionmaki
ngst
atement
s–
i
f.
..
elsest
atement−uset
hisst
atementi
fyouwantt
oexecut
easetofcode
whenacondi
ti
oni
str
ueandanot
heri
fthecondi
ti
oni
snott
rue
el
sei
fst
atement−i
susedwi
tht
hei
f.
..
elsest
atementt
oexecut
easetofcodei
f
oneoft
hesev
eral
condi
ti
oni
str
ue
swi
tchst
atement−i
susedi
fyouwantt
osel
ectoneofmanybl
ocksofcodet
o
beexecut
ed,uset
heSwi
tchst
atement
.Theswi
tchst
atementi
susedt
oav
oid
l
ongbl
ocksofi
f.
.el
sei
f.
.el
secode.
TheI
f.
..
ElseSt
atement
I
fyouwantt
oexecut
esomecodeifacondi
ti
oni
str
ueandanot
hercodei
facondi
ti
on
i
sfal
se,
usethei
f. elsest
atement.
NehaMi
dha,
Lect
urer GPPanchkul
a
Synt
ax
i
f(condi
ti
on)
codetobeexecut
edi
fcondi
ti
oni
strue;
el
se
codetobeexecut
edi
fcondi
ti
oni
sfalse;
Exampl
e
Thefol
lowi
ngexampl
ewil
lout
put"Haveaniceweekend!
"ift
hecur
rentdayi
sFr
iday
,
Other
wise,
itwi
l
lout
put"
Haveaniceday!
":
<ht
ml>
<body
>
<?
php
$d=dat
e("
D")
;
i
f($d=="Fr
i"
)
echo"
Haveani
ceweekend!
";
el
se
echo"
Hav
eani
ceday
!";
?
>
</body
>
</
html>
I
twi
l
lpr
oducet
hef
oll
owi
ng
r
esul
t−Hav
eani
ceweekend!
TheEl
seI
fSt
atement
Ifyouwanttoexecut
esomecodei
foneoft
hesev
eral
condi
ti
onsar
etr
ueuset
he
elsei
fst
atement
Synt
ax
i
f(condi
ti
on)
codetobeexecut
edi
fcondi
ti
oni
strue;
el
sei condi
f( ti
on)
codetobeexecut
edi
fcondi
ti
oni
strue;
el
se
codetobeexecut
edi
fcondi
ti
oni
sfalse;
Exampl
e
Thef
oll
owingexampl
ewill
output"
Haveaniceweekend!
"ift
hecurrentdayisFr
iday
,
and"
HaveaniceSunday
!"i
fthecur
rentdayi
sSunday.Ot
herwise,
itwil
loutput"
Havea
ni
ceday!
"
−
NehaMi
dha,
Lect
urer GPPanchkul
a
<ht
ml>
<body
>
<?
php
$d=dat
e("
D")
;
i
f($d=="Fr
i"
)
echo"
Haveani
ceweekend!
";
el
sei
f($d=="Sun")
echo"Hav
eani ceSunday
!";
el
se
echo"
Hav
eani
ceday
!";
?
>
</body
>
</
html>
I
twi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
Hav
eani
ceWeekend!
TheSwi
tchSt
atement
I
fyouwantt
osel
ectoneofmanybl
ocksofcodet
obeexecut
ed,
uset
heSwi
tch
st
atement
.Theswi
tchst
atementi
susedt
oav
oidl
ongbl
ocksofi
f.
.el
sei
f.
.el
secode.
Syntax
switch
(expressi
on)
{case
label1:
codetobeexecut
edi
fexpr
essi
on=l
abel
1;
break;
caselabel
2:
codet obeexecut
edi
fexpr
essi
on=l
abel
2;
break;
default
:
codetobeexecuted
i
fexpressi
onisdif
fer
ent
fr
om bothlabel
1and
l
abel2;
}
NehaMi
dha,
Lect
urer GPPanchkul
a
Exampl
e
Theswitchstatementwor ksinanunusualway.Fir
stitevaluatesgi
venexpressi
onthen
seeksalabletomat chther esult
ingval
ue.I
famat chi
ngv alueisf
oundthent hecode
associ
atedwi t
ht hemat chi
ngl abelwil
lbeexecutedori fnoneoft helablematches
thenst
atementwi ll
executeanyspecifi
eddefaul
tcode.
<ht
ml>
<body
>
<?
php
$d=dat
e("
D")
;
switch
($d){case
"Mon" :
echo"Todayis
Monday";br
eak;
case"
Tue":
echo"Todayis
Tuesday"
;break;
case"
Wed" :
echo"Todayis
Wednesday";
break;
case"
Thu":
echo"Todayis
Thursday
";br
eak;
case"Fr
i"
:
echo"Todayis
Fri
day";br
eak;
case"Sat":
echo" Todayis
Saturday";
break;
case"
Sun":
echo"Todayis
Sunday"
;break;
def
ault
:
echo"Wonderwhi
chdayi
sthi
s?"
;
}
?
>
NehaMi
dha,
Lect
urer GPPanchkul
a
</body
>
</
html>
I
twi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
Todayi
sMonday
NehaMi
dha,
Lect
urer GPPanchkul
a
Loopsi
nPHP
Loopsi
nPHPar eusedt
oexecutethesameblockofcodeaspeci
fi
ednumberof
ti
mes.PHPsupport
sfol
l
owingfourloopt
ypes.
f
or−l
oopst
hroughabl
ockofcodeaspeci
fi
ednumberoft
imes.
whi
le−l
oopst
hroughabl
ockofcodei
fandasl
ongasaspeci
fi
edcondi
ti
oni
str
ue.
do.
..
whi
le−l
oopst
hroughabl
ockofcodeonce,
andt
henr
epeat
sthel
oopas
l
ongasaspeci
alcondi
ti
oni
str
ue.
f
oreach−l
oopst
hroughabl
ockofcodef
oreachel
ementi
nanar
ray
.
Wewi
l
ldi
scussaboutcont
inueandbr
eakkey
wor
dsusedt
ocont
rol
thel
oopsexecut
ion.
Thef
orl
oopst
atement
Theforst
atementisusedwhenyouknowhowmanyt
imesy
ouwantt
oexecut
ea
st
atementorablockofstat
ements.
Syntax
for(i
nit
ial
i
zati
on;condi
ti
on;
i
ncrement{codet
) obeexecut
ed;
}
Theiniti
ali
zerisusedtosett
hest
artval
uefort
hecounteroft
henumberofloop
i
ter
ations.Av ari
ablemaybedecl
aredher
efort
hispur
poseanditi
str
adit
ionalt
o
namei t$i.
NehaMi
dha,
Lect
urer GPPanchkul
a
Exampl
e
Thefol
lowi
ngexamplemakesfi
vei
ter
ati
onsandchangest
heassi
gnedv
alueoft
wo
var
iabl
esoneachpassoft
heloop−
<ht
ml>
<body
>
<?
php
$a=0;
$b=0;
f
or($i
=0;$i
<5;
$i++){
$a+=10;
$b+=5;
}
echo(
"Att
heendoft
hel
oopa=$aandb=$b")
;
?
>
</body
>
</
html>
Thi
swi
l
lpr
oducet
hef
oll
owi
ng
r
esul
t−Att
heendoft
hel
oopa=50
andb=25
Thewhi
l
eloopst
atement
Thewhi
l
est
atementwi
l
lexecut
eabl
ockofcodei
fandasl
ongasat
estexpr
essi
oni
str
ue.
I
fthetestexpressi
onistr
uethenthecodebl
ockwillbeexecut
ed.Af
terthecode
hasexecutedthet estexpr
essi
onwi l
lagai
nbeevaluat
edandt hel
oopwi l
lconti
nue
unt
ilt
hetestexpressioni
sfoundtobefal
se.
NehaMi
dha,
Lect
urer GPPanchkul
a
Synt
ax
whil
e(condi
ti
on){
codetobeexecuted;
}
Exampl
e
Thisexampl
edecr
ement
sav ar
iabl
eval
ueoneachit
erat
ionofthel
oopandthe
counteri
ncr
ement
sunt
ili
treaches10whentheeval
uati
onisfal
seandt
heloopends.
<ht
ml>
<body
>
<?
php
$i=0;
$num =50;
whi
le($i
<10){
$num--
;
$i
++;
}
echo(
"Loopst
oppedati
=$i
andnum =$num")
;
?
>
</body
>
</
html>
Thi
swi
l
lpr
oducet
hef
oll
owi
ng
r
esul
t−Loopst
oppedati
=10and
num =40
Thedo.
..
whi
lel
oopst
atement
Thedo.
..
whi
l
est at
ementwi
ll
execut
eablockofcodeatl
eastonce-i
tthenwi
l
l
r
epeatthel
oopaslongasacondi
ti
onistr
ue.
Synt
a
xdo{
codet
obeexecut
ed;
}
whil
e
condi
( ti
on)
;
Exampl
e
Thefol
lowi
ngexampl
ewill
incr
ementt
heval
ueofiatl
eastonce,
andi
twi
l
lcont
inue
i
ncrementi
ngt
hevari
abl
eiaslongasi
thasaval
ueofl
essthan10−
NehaMi
dha,
Lect
urer GPPanchkul
a
<ht
ml>
<body
>
<?
php
$i=0;
$num =0;
do{
$i
++;
}
whil
e($i<10)
;
echo("
Loopst
oppedati
=$i
");
?
>
</body
>
</
html>
Thi
s wi
l
l pr
oduce t
he f
oll
owi
ng
r
esul
t−Loopst
oppedati
=10
Thef
oreachl
oopst
atement
Theforeachstat
ementi
susedtoloopthrougharr
ays.Foreachpasst
heval
ueofthe
cur
rentarrayel
ementi
sassi
gnedto$valueandthearraypoi
nteri
smovedbyoneand
i
nthenextpassnextel
ementwil
lbepr
ocessed.
Sy nt
ax
foreach(ar
rayasvalue){
codetobeexecuted;
}
Exampl
e
Tr
youtf
oll
owi
ngexampl
etol
i
stoutt
hev
aluesofanar
ray
.
<ht
ml>
<body
>
<?
php
$ar
ray=ar
ray
(1,
2,3,
4,5)
;
foreach($ar
rayas$value)
{echo"Val
ueis$value<br
/>"
;
}
?
>
NehaMi
dha,
Lect
urer GPPanchkul
a
</body
>
</
html>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
Val
uei
s1
Val
uei
s2
Val
uei
s3
Val
uei
s4
Val
uei
s5
Thebr
eakst
atement
ThePHPbr
eakkey
wor
disusedt
oter
minat
etheexecut
ionofal
ooppr
emat
urel
y.
Thebreakstat
ementissi
tuatedi
nsi
dethestat
ementbl
ock.I
tgi
vesyouful
lcont
rol
and
wheneveryouwanttoexi
tfrom t
heloopyoucancomeout.Aft
ercomi
ngoutofaloop
i
mmedi at
estatementt
otheloopwil
lbeexecut
ed.
Exampl
e
I
nthef
oll
owingexamplecondi
ti
ont
estbecomest
ruewhent
hecount
erv
aluer
eaches
3andl
ooptermi
nates.
<ht
ml>
<body
>
<?
php
$i=0;
whi
l
e($i<10){
$i
++;
NehaMi
dha,
Lect
urer GPPanchkul
a
if($i
==3) br
eak;
}
echo("Loopst
oppedati
=$i
");
?
>
</body
>
</
html>
Thi
s wi
l
l pr
oduce t
he f
oll
owi
ng
r
esul
t−Loopst
oppedati
=3
Thecont
inuest
atement
ThePHPconti
nuekey
wor
disusedt
ohal
tthecur
renti
ter
ati
onofal
oopbuti
tdoesnot
ter
minat
ethel
oop.
Justli
kethebr eakstat
ementtheconti
nuestatementissit
uat
edinsi
dethestat
ement
blockcontai
ningthecodethattheloopexecutes,pr
ecededbyacondit
ionalt
est.For
thepassencount er
ingcont
inuestat
ement,restoftheloopcodeisski
ppedandnext
passstart
s.
Exampl
e
I
nthefoll
owingexampl
elooppri
ntst
heval
ueofarraybutf
orwhi
chcondi
ti
onbecomes
t
ruei
tjustskipt
hecodeandnextval
uei
spri
nted.
<ht
ml>
<body
>
<?
php
$ar
ray=ar
ray
(1,
2,3,
4,5)
;
f
oreach($ar r
ayas$v
alue
){if
($value==3
)cont
inue;
NehaMi
dha,
Lect
urer GPPanchkul
a
echo"
Val
uei
s$v
alue<br/
>";
}
?
>
</body
>
</
html>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
Val
uei
s1
Val
uei
s2
Val
uei
s4
Val
uei
s5
NehaMi
dha,
Lect
urer GPPanchkul
a
Ar
ray
Anarr
ayisadatastr
uctur
ethatst
oresoneormoresimilart
ypeofval
uesina
si
ngl
ev al
ue.Forexampl
eifyouwanttostore100number
st heninst
eadofdef
ini
ng
100vari
abl
esit
seasytodef
ineanar
rayof100lengt
h.
Ther
earethr
eedi
ff
erentki
ndofar
ray
sandeachar
rayv
aluei
saccessedusi
nganI
D
cwhi
chiscal
l
edarr
ayindex.
Numer
icar
ray−Anar
raywi
thanumer
ici
ndex.Val
uesar
est
oredandaccessed
i
nli
nearf
ashi
on.
Associ
ati
vear
ray−Anar
raywi
thst
ri
ngsasi
ndex.Thi
sst
oresel
ementv
aluesi
n
associ
ati
onwi
thkeyv
aluesr
athert
hani
nast
ri
ctl
i
neari
ndexor
der
.
Mul
ti
dimensi
onalar
ray−Anar
raycont
aini
ngoneormor
ear
ray
sandv
aluesar
e
accessedusi
ngmul
ti
plei
ndi
ces
Numer
icAr
ray
Thesearr
ayscanst
orenumbers,st
ringsandanyobj
ectbuttheiri
ndexwi
l
lbe
repr
esent
edbynumbers.Bydefaul
tarrayi
ndexst
art
sfrom zero.
Exampl
e
Fol
l
owi
ngi
stheexampl
eshowi
nghowt
ocr
eat
eandaccessnumer
icar
ray
s.
Her
ewehav eusedarr
ay(
)funct
iont
ocr
eat
ear
ray
.Thi
sfunct
ioni
sexpl
ainedi
n
f
uncti
onr
eference.
<ht
ml>
<body
>
<?
php
/*Fi
rstmet
hodtocreat
earr
ay.*/
$numbers=ar
ray(1,2,
3,4,
5);
f
oreach($numbersas$value)
{echo"Valuei
s$value<br
/>"
;
}
/*Secondmethodt ocreat
ear
ray
.*/
$numbers[
0]="one";
$numbers[
1]="two";
$numbers[
2]="three"
;
$numbers[
3]="four";
$numbers[
4]="fi
ve";
f
oreach($number
sas$v
alue){
NehaMi
dha,
Lect
urer GPPanchkul
a
echo"
Val
uei
s$v
alue<br/
>";
}
?
>
</body
>
</
html>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
Valuei
s1
Valuei
s2
Valuei
s3
Valuei
s4
Valuei
s5
Valuei
sone
Valuei
stwo
Valuei
s
thr
eeValue
i
sfourValue
i
sfive
Associ
ati
veAr
ray
s
Theassociati
vearr
aysarever
ysimil
artonumer i
carraysinter
m offuncti
onal
it
ybut
theyarediff
erenti
ntermsofthei
rindex.Associ
ati
vear r
aywillhav
et hei
rindexas
stri
ngsothatyoucanest
abl
i
shastrongassoci
ati
onbet weenkeyandval
ues.
Tostorethesalariesofemploy
eesinanarr
ay,anumer i
cal
l
yindexedarr
aywoul
dnot
bethebestchoi ce.Inst
ead,wecoul
dusetheempl oyeesnamesast hekey
sinour
associ
ati
vearr
ay ,andtheval
uewoul
dbethei
rrespect
ivesal
ary
.
NOTE−Don'tkeepassoci
ati
vear
rayi
nsi
dedoubl
equot
ewhi
l
epr
int
ingot
her
wisei
t
woul
dnotr
etur
nanyvalue.
Exampl
e
<ht
ml>
<body
>
<?
php
/*Fi
rstmet
hodtoassoci
atecr
eat
ear
ray
.*/
$sal
ari
es=arr
ay(
"mohammad"=>2000,
"qadi
r"=>1000,
"zar
a"=>500)
;
echo"Salar
yofmohammadi s".$salari
es['
mohammad' ].
"<br/
>";echo"
Salaryofqadiri
s".$salari
es['
qadi
r'
]."
<br/>"
;
echo"Salar
yofzarais".$sal
ari
es['
zara'
]."
<br/>"
;
/*Secondmethodt
ocreatearr
ay.*
/
$sal
ari
es['
mohammad'
]="high";
$sal
ari
es['
qadi
r'
]="
medium";
NehaMi
dha,
Lect
urer GPPanchkul
a
$sal
ari
es[
'
zar
a']="
low"
;
echo"Salar
yofmohammadi s".$salari
es['
mohammad' ].
"<br/
>";echo"
Salaryofqadiri
s".$salari
es['
qadi
r'
]."
<br/>"
;
echo"Salar
yofzarais".$sal
ari
es['
zara'
]."
<br/>"
;
?
>
</body
>
</
html>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
Salar
yofmohammadi s
2000Salar
yofqadiri
s
1000Salaryofzar
ais500
Salar
yofmohammadi s
hi
ghSalaryofqadi
ris
medium Salar
yofzar
ais
l
ow
Mul
ti
dimensi
onalAr
ray
s
Amul ti
-di
mensi
onalarr
ayeachel ementi
nt hemainarr
aycanal sobeanarr
ay.And
each el
ementint hesub-arraycan bean arr
ay,and so on.Valuesi
nthemulti
-
dimensi
onalar
rayareaccessedusingmul
ti
plei
ndex.
Exampl
e
I
nthisexamplewecr
eat
eat
wodi
mensi
onalar
rayt
ost
oremar
ksoft
hreest
udent
sin
t
hreesubject
s−
Thi
sexampl
eisanassoci
ati
vear
ray
,youcancr
eat
enumer
icar
rayi
nthesamef
ashi
on.
<ht
ml>
<body
>
<?
php
$marks=
arr
ay(" mohammad"
=>array(
"physics"=>35,
"mat hs"=>30,
"chemi st
ry"=>39
),
"
qadir"=>arr
ay
("physi
cs"=>
30,
"maths"=>32,
"chemist
ry"=>29
)
,
NehaMi
dha,
Lect
urer GPPanchkul
a
"zara"=>array
("physi
cs"=>
31,
"maths"=>22,
"chemist
ry"=>39
)
)
;
/*Accessingmulti
-di
mensionalar
ray
val
ues*/echo" Marksformohammadi n
physi
cs: ";echo
$marks['
mohammad' ][
'phy
sics'
]."
<br/>";
echo"Mar
ksforqadiri
nmaths: "
;
echo$marks[
'
qadir
']
['
maths'
]."
<br
/>"
;
echo"Marksforzarai
nchemistry:";
echo$mar ks[
'
zara'
]
['
chemi
stry'
]."<br
/>"
;
?>
Thi
swill
producethefol
lowi
ngresult−
Mar
ksformohammadinphysi
cs:
35Mar
ksforqadi
rinmat
hs:32
Mar
ksforzar
ainchemi
str
y:39
NehaMi
dha,
Lect
urer GPPanchkul
a
St
ri
ngsi
nPHP
Theyar
esequencesofchar
act
ers,
li
ke"
PHPsuppor
tsst
ri
ng
oper
ati
ons"
.Fol
l
owi
ngar
eval
i
dex
ampl
esofst
ri
ng
$st
ri
ng_
1="Thisisastri
ngindoublequotes";
$st
ri
ng_
2="Thisisasomewhatlonger ,si
ngl
yquotedst
ri
ng"
;
$st
ri
ng_
39="Thisstr
inghasthi
rt
y-ninecharact
ers"
;
$st
ri
ng_
0="";
//astringwit
hzerochar act
ers
Singlyquot
edstri
ngsar
etreat
edal
mostl
it
eral
ly
,whereasdoubl
yquot
edstr
ings
replacevar
iabl
eswit
hthei
rval
uesaswel
lasspeci
all
yint
erpr
eti
ngcer
tai
ncharact
er
sequences.
<?
php
$vari
abl
e="name"
;
$li
ter
all
y='
My$vari
abl
ewi
l
lnotpr
int
!\\
n';
pr
int(
$lit
eral
l
y);
pr
int"<br/>"
;
$l
i
ter
all
y="
My$v
ari
abl
ewi
l
l
pr
int
!\\
n";
pri
nt(
$li
ter
all
y);
?
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
My$variabl
ewi
l
lnot
pr
int
!\nMynamewil
l
pr
int
!\n
Ther
earenoar
ti
fi
cial
li
mit
sonstr
inglength-wi
thintheboundsofav
ail
abl
ememor
y,
yououghtt
obeabl
etomakearbi
trar
il
ylongstr
ings.
Str
ingst
hatar
edeli
mit
edbydoubl
equot
es(
asi
n"t
his"
)ar
epr
epr
ocessedi
nbot
hthe
fol
l
owingtwowaysbyPHP−
Cer
tai
nchar
act
ersequencesbegi
nni
ngwi
thbacksl
ash(
\)ar
erepl
acedwi
th
speci
alchar
act
ers
Var
iabl
enames(
star
ti
ngwi
th$)ar
erepl
acedwi
thst
ri
ngr
epr
esent
ati
onsof
t
hei
rval
ues.Theescape-
sequencer
epl
acement
sar
e−
\
nisr
epl
acedbyt
henewl
i
nechar
act
er
\
risr
epl
acedbyt
hecar
ri
age-
ret
urnchar
act
er
\
tisr
epl
acedbyt
het
abchar
act
er
\
$isr
epl
acedbyt
hedol
l
arsi
gni
tsel
f($)
\
"isr
epl
acedbyasi
ngl
edoubl
e-quot
e("
)
\
\isr
epl
acedbyasi
ngl
ebacksl
ash(
\)
NehaMi
dha,
Lect
urer GPPanchkul
a
St
ri
ngConcat
enat
ionOper
ator
Toconcat
enat
etwost
ri
ngv
ari
abl
est
oget
her
,uset
hedot(
.)oper
ator−
<?
php
$st
ri
ng1="
Hell
oWorl
d";
$st
ri
ng2="
1234";
echo$st
ri
ng1."".$st
ri
ng2;
?
>
Thi
swi
l
lpr
oducet
hef
oll
owi
ng
r
esul
t−Hel
l
oWor
ld1234
I
fwel ookatt
hecodeaboveyouseethatweusedt
heconcat
enat
ionoper
atort
wot
imes.Thi
s
i
s
becausewehadtoinser
tathi
rdst
ri
ng.
Betweenthet
wost r
ingvar
iabl
esweaddedast
ri
ngwi
thasi
ngl
echar
act
er,
anempt
y
space,t
oseparat
ethetwov ar
iabl
es.
Usi
ngt
hest
rl
en(
)funct
ion
Thest
rl
en(
)funct
ioni
susedt
ofi
ndt
hel
engt
hofa
st
ri
ng.Let
'sf
indt
hel
engt
hofourst
ri
ng"
Hel
l
owor
ld!
"−
<?php
echost
rl
en(
"Hel
l
owor
ld!
")
;
?>
Thi
swi
l
lpr
oducet
hef
oll
owi
ng
r
esul
t−12
Thelengt
hofastri
ngisoft
enusedinl
oopsorot
herfunct
ions,
whenitisi
mpor
tantt
oknow
whenthestri
ngends.(
i.
e.i
naloop,
wewouldwanttostoptheloopaf
tert
hel
ast
char
acteri
nthestr
ing)
Usi
ngt
hest
rpos(
)funct
ion
Thest
rpos(
)funct
ioni
susedt
osear
chf
orast
ri
ngorchar
act
erwi
thi
nast
ri
ng.
I
famatchisfoundinthest
ring,t
hisf
unct
ionwi
l
lret
urnt
heposi
ti
onoft
hef
ir
stmat
ch.
I
fnomatchisfound,i
twil
lretur
nFALSE.
Let
'sseei
fwecanf
indt
hest
ri
ng"
wor
ld"i
nourst
ri
ng−
<?php
echost
rpos(
"Hel
l
owor
ld!
",
"wor
ld"
);
?>
NehaMi
dha,
Lect
urer GPPanchkul
a
Thi
swi
l
lpr
oducet
hef
oll
owi
ng
r
esul
t−6
Asyouseetheposit
ionofthestri
ng"
worl
d"inourstri
ngi
sposit
ion6.Ther
easont
hati
tis6,
andnot7,
isthatt
hef i
rstposi
ti
onint
hestr
ingi
s0, andnot1.
NehaMi
dha,
Lect
urer GPPanchkul
a
GET&POSTMet
hods
Ther
ear
etwoway
sthebr
owsercl
i
entcansendi
nfor
mat
iont
othewebser
ver
.
TheGETMet
hod
ThePOSTMet
hod
Bef
oret
hebr
owsersendst
hei
nfor
mat
ion,i
tencodesi
tusi
ngaschemecal
l
edURL
encodi
ng.I
nthi
sscheme,name/
val
uepai
rsar
ejoi
nedwi
thequalsi
gnsanddi
ff
erent
pai
rsar
esepar
atedbyt
heamper
sand.
name1=v
alue1&name2=v
alue2&name3=v
alue3
Spacesar
eremov
edandr
epl
acedwi
t he+char
ht act
erandanyot
hernonal
phanumer
ic
char
act
ersar
erepl
acedwi
thahex
adeci
malv
alues.Af
tert
hei
nfor
mat
ioni
sencodedi
t
i
ssentt
otheser
ver
.
TheGETMet
hod
TheGETmet
hodsendst
heencodeduseri
nfor
mat
ionappendedt
othepager
equest
.
Thepageandt
heencodedi
nfor
mat
ionar
esepar
atedbyt
he?char
act
er.
ht
tp:
//www.
test
.com/
index.
htm?
name1=v
alue1&name2=v
alue2
TheGETmet
hodpr
oducesal
ongst
ri
ngt
hatappear
siny
ourser
verl
ogs,
int
he
br
owser
'sLocat
ion:
box.
TheGETmet
hodi
srest
ri
ctedt
osendupt
o1024char
act
ersonl
y.
NeveruseGETmet
hodi
fyouhav
epasswor
dorot
hersensi
ti
vei
nfor
mat
iont
obe
sentt
otheser
ver
.
GETcan'
tbeusedt
osendbi
nar
ydat
a,l
i
kei
magesorwor
ddocument
s,t
otheser
ver
.
Thedat
asentbyGETmet
hodcanbeaccessedusi
ngQUERY_
STRI
NG
env
ironmentv
ari
abl
e.
ThePHPpr
ovi
des$_
GETassoci
ati
vear
rayt
oaccessal
lthesenti
nfor
mat
ion
usi
ngGETmet
hod.
NehaMi
dha,
Lect
urer GPPanchkul
a
Tr
youtf
oll
owi
ngexampl
ebyput
ti
ngt
hesour
cecodei
ntest
.phpscr
ipt
.
<?
php
i
f($_GET[
"name"
]||$_
GET["
age"]){
echo"Welcome".$_
GET['
name']."
<br/
>";
echo"Youare"
.$_GET[
'
age']
."yearsol
d."
;
exi
t(
);
}
?>
<ht
ml >
<body>
<f
orm act i
on=" <?php$_PHP_SELF? >"method=
"GET" >Name: <inputty
pe="text
"name=" name"/
>
Age: <inputt
ype=" text
"name=" age"/>
<inputt y
pe=" submit"/>
</
form>
</body
>
</
html>
I
twi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
ThePOSTMet
hod
ThePOSTmet
hodt
ransf
ersi
nfor
mat
ionv
iaHTTPheader
s.Thei
nfor
mat
ioni
s
encodedasdescr
ibedi
ncaseofGETmet
hodandputi
ntoaheadercal
l
ed
QUERY_
STRI
NG.
ThePOSTmet
hoddoesnothav
eanyr
est
ri
cti
onondat
asi
zet
obesent
.
ThePOSTmet
hodcanbeusedt
osendASCI
Iaswel
lasbi
nar
ydat
a.
Thedat
asentbyPOSTmet
hodgoest
hroughHTTPheadersosecur
it
ydepends
onHTTPpr
otocol
.Byusi
ngSecur
eHTTPy
oucanmakesur
ethaty
our
i
nfor
mat
ioni
ssecur
e.
ThePHPpr
ovi
des$_
POSTassoci
ati
vear
rayt
oaccessal
lthesenti
nfor
mat
ion
usi
ngPOSTmet
hod.
Tr
youtf
oll
owi
ngexampl
ebyput
ti
ngt
hesour
cecodei
ntest
.phpscr
ipt
.
NehaMi
dha,
Lect
urer GPPanchkul
a
<?
php
i
f($_POST[ "
name"]||$_POST["
age"
]){
if(preg_
mat ch(
"/[^
A-Za-
z'-
]/
",
$_POST['name'])
)
{die("
inval
idnameandnameshoul dbe
alpha"
);
}
echo" Welcome" .$_POST['
name'
]."<br/>"
;
echo" Youare".$_POST['
age']
."yearsold.
";
exi
t(
);
}
?>
<ht
ml >
<body>
<f
orm action="<?php$_PHP_ SELF? >"method=
"POST">Name: <i
nputtype="text"name=" name"/
>
Age: <i
nputty
pe=" text
"name=" age"/>
<inputtype="submit"/
>
</
form>
</body
>
</ht
ml>
Itwil
lpr
oducet
hef
oll
owi
ngr
esul
t−
The$_
REQUESTv
ari
abl
e
ThePHP$_REQUESTvar
iabl
econt
ainsthecont
ent
sofbot
h$_GET,$_POST,and
$_COOKI
E.Wewil
ldi
scuss$_COOKI
Ev ari
abl
ewhenwewil
lexpl
ainaboutcooki
es.
ThePHP$_REQUESTv
ari
abl
ecanbeusedt
ogett
her
esul
tfr
om f
orm dat
asentwi
th
bot
htheGETandPOSTmethods.
Tr
youtf
oll
owi
ngexampl
ebyput
ti
ngt
hesour
cecodei
ntest
.phpscr
ipt
.
<?php
if($_ REQUEST["
name"]||$_REQUEST["
age"])
{echo" Welcome".$_REQUEST['
name']."<br
/>";echo"Youare"
.$_REQUEST['
age'
]
."y ears
old."
;exi
t(
);
}
?>
<html>
<body >
NehaMi
dha,
Lect
urer GPPanchkul
a
<f
orm action="<?php$_PHP_ SELF? >"method=
"POST">Name: <i
nputtype="text"name=" name"/
>
Age: <i
nputty
pe=" text
"name=" age"/>
<inputtype="submit"/
>
</
form>
</body
>
</
html>
Her
e$_
PHP_
SELFv
ari
abl
econt
ainst
henameofsel
fscr
ipti
nwhi
chi
tisbei
ng
cal
l
ed.I
twi
l
lpr
oducet
hef
oll
owi
ngr
esul
t−
NehaMi
dha,
Lect
urer GPPanchkul
a
Funct
ions
PHPfunct
ionsar esi
milart
ootherpr
ogrammingl
anguages.Afunct
ioni
sapiece
ofcode which takes one moreinputinthe f
orm ofparameterand doessome
pr
ocessingandreturnsaval
ue.
Youal
readyhaveseenmanyf
unct
ionsl
ikefopen()andf
read()et
c.Theyare
bui
l
t-i
nfunct
ionsbutPHPgivesy
ouopti
ontocreat
ey ourownfunct
ionsaswell
.
Ther
ear
etwopar
tswhi
chshoul
dbecl
eart
oyou−
Cr
eat
ingaPHPFunct
ion
Cal
li
ngaPHPFunct
ion
I
nfactyouhardlyneedtocreateyourownPHPfunct
ionbecauset
her
earealr
eady
mor
ethan1000ofbui l
t-
inli
braryf
uncti
onscreat
edf
ordi
ffer
entar
eaandyouj
ustneed
t
ocall
them accor
dingtoyourrequi
rement.
Pl
easer
efert
oPHPFunct
ionRef
erencef
oracompl
etesetofusef
ulf
unct
ions.
Cr
eat
ingPHPFunct
ion
It
sv eryeasytocr
eateyourownPHPf unct
ion.Supposey ouwantt ocreat
eaPHP
functi
onwhichwi
ll
simpl
ywrit
easimplemessageony ourbrowserwhenyouwill
cal
lit
.
Foll
owingexampl
ecreat
esafunct
ioncal
ledwrit
eMessage()andthencal
lsitj
ustaf
ter
creat
ingit
.
Notethatwhi
l
ecr
eati
ngaf
unct
ionit
snameshoul
dstar
twit
hkey
wor
dfunct
ionandal
l
thePHPcodeshoul
dbeputi
nside{and}br
acesasshownint
hef
oll
owi
ngexample
below−
<ht
ml>
<head>
<tit
le>Wr
it
ingPHPFunct
ion</
ti
tl
e>
</head>
<body
>
<?
php
/*Defini
ngaPHPFunct ion*/
functi
onwr i
teMessage(
){
echo"Youarereal
lyaniceper
son,
Hav
eani
cet
ime!
";
}
/*Cal
li
ngaPHPFuncti
on
*/wri
teMessage(
);
?
>
</
body
>
NehaMi
dha,
Lect
urer GPPanchkul
a
</
html
>
Thi
swi
l
ldi
spl
ayf
oll
owi
ngr
esul
t−
Youar
ereal
l
yani
ceper
son,
Hav
eani
cet
ime!
PHPFunct
ionswi
thPar
amet
ers
<head>
<tit
le>Wr
it
ingPHPFunct
ionwi
thPar
amet
ers</
ti
tl
e>
</head>
<body
>
<?
php
functi
onaddFuncti
on(
$num1,$num2){
$sum =$num1+$num2;
echo"Sum oft
hetwonumbersis:
$sum"
;
}
addFunct
ion(
10,
20)
;
?
>
</body
>
</
html>
Thi
swi
l
ldi
spl
ayf
oll
owi
ngr
esul
t
−Sum oft
het
wonumber
sis:
30
Passi
ngAr
gument
sbyRef
erence
I
tispossi
bletopassargumentst
ofunct
ionsbyref
erence.Thi
smeanst
hataref
erence
t
othev ar
iabl
eismanipul
atedbyt
hefuncti
onrat
herthanacopyoft
hevari
abl
e'
svalue.
Anychangesmadetoanar
gumentint
hesecaseswillchanget hev
alueoftheor
igi
nal
var
iabl
e.Youcanpassanargumentbyr ef
erencebyaddi nganamper sandtothe
var
iabl
enameinei
thert
hef
unct
ioncal
lort
hefuncti
ondefini
tion.
Fol
lowi
ngexampl
edepi
ctsbot
hthecases.
<ht
ml>
<head>
NehaMi
dha,
Lect
urer GPPanchkul
a
<ti
tl
e>Passi
ngAr
gumentbyRef
erence</
ti
tl
e>
</
head>
<body
>
<?
php
functi
onaddFi
ve(
$num){
$num +=5;
}
f
uncti
onaddSix(
&$num){
$num +=6;
}
$ori
gnum =10;
addFi
ve($or
ignum )
;
echo"
Ori
ginal
Val
uei
s$or
ignum<br
/>"
;addSi
x($orignum )
;
echo"Ori
ginal
Val uei
s$or
ignum<br/
>";
?
>
</body
>
</
html>
Thi
swi
l
ldi
spl
ayf
oll
owi
ngr
esul
t−
Ori
ginalValuei
s
10Or i
ginal
Value
i
s16
PHPFunct
ionsr
etur
ningv
alue
Afuncti
oncanr
etur
nav al
ueusingt
her et
urnstat
ementinconj
uncti
onwit
havalueor
obj
ect.r
etur
nst
opstheexecut
ionofthefunct
ionandsendstheval
uebacktot
he
cal
li
ngcode.
Youcanr
etur
nmor
ethanonev
aluef
rom af
unct
ionusi
ngr
etur
nar
ray
(1,
2,
3,
4).
Fol
lowingexampl
et akestwoint
egerparameter
sandaddthem t
oget
herandt
hen
ret
urnsthei
rsum t
ot hecall
i
ngprogram.Notethatr
etur
nkeywor
disusedtor
etur
na
<ht
ml>
<head>
<tit
le>Wr
it
ingPHPFunct
ionwhi
chr
etur
nsv
alue</
ti
tl
e>
</head>
<body
>
v
aluef
rom af
unct
ion.
NehaMi
dha,
Lect
urer GPPanchkul
a
<?
php
functi
onaddFunct
ion(
$num1,$num2){
$sum =$num1+$num2;
ret
urn$sum;
}
$return_
val
ue=addFunct
ion(
10,20)
;
echo"
Ret
urnedv
aluef
rom t
hef
unct
ion:
$ret
urn_
val
ue"
;
?
>
</body
>
</
html>
Thi
swi
l
ldi
spl
ayf
oll
owi
ngr
esul
t−
Ret
urnedv
aluef
rom t
hef
unct
ion:
30
Set
ti
ngDef
aul
tVal
uesf
orFunct
ionPar
amet
ers
Youcansetapar
amet
ert
ohav
eadef
aul
tval
uei
fthef
unct
ion'
scal
l
erdoesn'
tpass
i
t.Fol
l
owi
ngf
unct
ionpr
int
sNULLi
ncaseusedoesnotpassanyv
aluet
othi
s
f
unct
ion.
<ht
ml>
<head>
<tit
le>Wr
it
ingPHPFunct
ionwhi
chr
etur
nsv
alue</
ti
tl
e>
</head>
<body
>
<?
php
funct
ionprintMe(
$par
am =NULL)
{pr
int$param;
}
print
Me("Thi
sis
test"
);pr
int
Me()
;
?
>
</body
>
</
html>
Thi
swi
l
lpr
oducef
oll
owi
ng
r
esul
t−Thi
sist
est
Dy
nami
cFunct
ionCal
ls
NehaMi
dha,
Lect
urer GPPanchkul
a
I
tispossi
bletoassi
gnfunct
ionnamesasstringst
ov ari
abl
esandthent
reatthese
v
ariabl
esexactl
yasyouwouldthefunct
ionnameitsel
f.Fol
lowi
ngexampledepict
s
t
hisbehavi
our.
<ht
ml>
<head>
<tit
le>Dy
nami
cFunct
ionCal
l
s</
ti
tl
e>
</head>
<body
>
<?
php
functi
onsay
Hello(
)
{echo"
Hell
o<br
/>"
;
}
$f
unct
ion_
hol
der="say
Hel
l
o";
$f
unct
ion_
hol
der(
);
?
>
</body
>
</
html>
Thiswil
ldi
spl
ayf
oll
owi
ngr
esul
t
−Hel
l
o
NehaMi
dha,
Lect
urer GPPanchkul
a
Cooki
es
Cooki
esar
etextf
il
esst
oredont
hecl
i
entcomput
erandt
heyar
ekeptofuse
t
racki
ngpur
pose.PHPt
ranspar
ent
lysuppor
tsHTTPcooki
es.
Ther
ear
ethr
eest
epsi
nvol
vedi
nident
if
yingr
etur
ninguser
s−
Ser
verscr
iptsendsasetofcooki
est
othebr
owser
.Forexampl
ename,age,or
i
dent
if
icat
ionnumberet
c.
Br
owserst
orest
hisi
nfor
mat
iononl
ocal
machi
nef
orf
utur
euse.
Whennextt
imebr
owsersendsanyr
equestt
owebser
vert
heni
tsendst
hose
cooki
esi
nfor
mat
iont
otheser
verandser
verusest
hati
nfor
mat
iont
oident
if
y
t
heuser
.
Set
ti
ngCooki
eswi
thPHP
PHPpr
ovi
dedset
cooki
e()f
unct
iont
osetacooki
e.Thi
sfunct
ionr
equi
resupt
o
si
xar
gument
sandshoul
dbecal
l
edbef
ore<ht
ml>t
ag.Foreachcooki
ethi
sfunct
ion
hast
obecal
l
edsepar
atel
y.
set
cooki
e(name,v
alue,expi
re,pat
h,domai
n,
secur
it
y);Her
eist
hedet
ailofal
lthear
gument
s
Name−Thi
sset
sthenameoft
hecooki
eandi
sst
oredi
nanenv
ironment
v
ari
abl
e cal
l
ed HTTP_
COOKI
E_VARS.Thi
svar
iabl
eis used whi
l
e accessi
ng
cooki
es.
Val
ue−Thi
sset
sthev
alueoft
henamedv
ari
abl
eandi
sthecont
entt
haty
ou
act
ual
l
ywantt
ost
ore.
Expi
ry−Thi
sspeci
fyaf
utur
eti
mei
nsecondssi
nce00:
00:
00GMTon1stJan
1970.Af
tert
hist
imecooki
ewi
l
lbecomei
naccessi
ble.I
fthi
spar
amet
eri
snot
sett
hencooki
ewi
l
laut
omat
ical
l
yexpi
rewhent
heWebBr
owseri
scl
osed.
Pat
h−Thi
sspeci
fi
est
hedi
rect
ori
esf
orwhi
cht
hecooki
eisv
ali
d.A si
ngl
e
f
orwar
dsl
ashchar
act
erper
mit
sthecooki
etobev
ali
dforal
ldi
rect
ori
es.
NehaMi
dha,
Lect
urer GPPanchkul
a
Domai
n−Thi
scanbeusedt
ospeci
fyt
hedomai
nnamei
nver
ylar
gedomai
ns
andmustcont
ainatl
eastt
woper
iodst
obev
ali
d.Al
lcooki
esar
eonl
yval
i
dfor
t
hehostanddomai
nwhi
chcr
eat
edt
hem.
Secur
it
y−Thi
scanbesett
o1t
ospeci
fyt
hatt
hecooki
eshoul
donl
ybesentby
secur
etr
ansmi
ssi
onusi
ngHTTPSot
her
wisesett
o0whi
chmeancooki
ecanbe
sentbyr
egul
arHTTP.
Fol
l
owi
ngexampl
ewi
l
lcr
eat
etwocooki
esnameandaget
hesecooki
eswi
l
lbe
expi
redaf
teronehour
.
<?php
setcooki
e("
name","JohnWatki
n",t
ime()
+3600,
"/"
,"
",0)
;
setcooki
e("
age"
,"36",t
ime(
)+3600,"
/",
"",
0);
?>
<html>
<head>
<tit
le>Set
ti
ngCooki
eswi
thPHP</
ti
tl
e>
</head>
<body>
<?phpecho"
SetCooki
es"
?>
</body
>
</
html
>
Accessi
ngCooki
eswi
thPHP
PHPpr
ovi
desmanyway
stoaccesscooki
es.Si
mpl
estwayi
stouseei
ther$_
COOKI
Eor
$HTTP_
COOKI
E_VARSv
ari
abl
es.Fol
l
owi
ngexampl
ewi
l
laccessal
lthecooki
esseti
nabov
e
exampl
e.
<ht
ml>
<head>
<tit
le>Accessi
ngCooki
eswi
thPHP</
ti
tl
e>
</head>
<body
>
<?
php
NehaMi
dha,
Lect
urer GPPanchkul
a
echo$_
COOKI
E["
name"
]."
<br/
>";
/*isequi
val
entt
o*/
echo$HTTP_COOKI
E_VARS[
"name"
]."
<br/
>";
echo$_
COOKI
E["
age"
]."
<br/
>";
/*isequi
val
entt
o*/
echo$HTTP_COOKI
E_VARS[
"age"
]."
<br/
>";
?
>
</body
>
</
html>
Youcanusei
sset
()f
unct
iont
ochecki
facooki
eissetornot
.
<ht
ml>
<head>
<tit
le>Accessi
ngCooki
eswi
thPHP</
ti
tl
e>
</head>
<body
>
<?
php
i
f(i
sset
($_COOKIE[
"name"
])
)
echo"Welcome".$_COOKIE[
"name"
]."
<br/
>";
el
se
echo"
Sor
ry.
..Notr
ecogni
zed"."
<br/
>";
?
>
</body
>
</
html>
Del
eti
ngCooki
ewi
thPHP
Of
fi
cial
l
y,t
odel
eteacooki
eyoushoul
dcal
lset
cooki
e()wi
tht
henamear
gumentonl
y
butt
hisdoesnotal
way
swor
kwel
l
,howev
er,
andshoul
dnotber
eli
edon.
I
tissaf
estt
osett
hecooki
ewi
thadat
ethathasal
readyexpi
red−
<?php
setcooki
e("
name","
",t
ime()-60,"
/",
""
,0)
;
setcooki
e("
age"
,""
,ti
me()-60,"/
",
"",
0);
?>
NehaMi
dha,
Lect
urer GPPanchkul
a
<ht
ml>
<head>
<tit
le>Del
eti
ngCooki
eswi
thPHP</
ti
tl
e>
</head>
<body>
<?phpecho"
Del
etedCooki
es"?
>
</body
>
</
html
>
NehaMi
dha,
Lect
urer GPPanchkul
a
Sessi
ons
Anal
ter
nat
ivewayt
omakedat
aaccessi
bleacr
osst
hev
ari
ouspagesofan
ent
ir
ewebsi
tei
stouseaPHPSessi
on.
Asessi
oncr
eat
esaf
il
einat
empor
arydi
rect
oryont
heser
verwher
eregi
ster
ed
sessi
onv
ari
abl
esandt
hei
rval
uesar
est
ored.Thi
sdat
awi
l
lbeav
ail
abl
etoal
lpageson
t
hesi
tedur
ingt
hatv
isi
t.
Thel
ocat
ionoft
het
empor
aryf
il
eisdet
ermi
nedbyaset
ti
ngi
nthephp.
inif
il
e
cal
l
edsessi
on.
sav
e_pat
h.Bef
oreusi
nganysessi
onv
ari
abl
emakesur
eyouhav
eset
up
t
hispat
h.
Whenasessi
oni
sst
art
edf
oll
owi
ngt
hingshappen−
PHPf
ir
stcr
eat
esauni
quei
dent
if
ierf
ort
hatpar
ti
cul
arsessi
onwhi
chi
sar
andom
st
ri
ngof32hexadeci
mal
number
ssuchas3c7f
oj34c3j
j
973hj
kop2f
c937e3443.
Acooki
ecal
l
edPHPSESSI
Disaut
omat
ical
l
ysentt
otheuser
'scomput
ert
ost
ore
uni
quesessi
oni
dent
if
icat
ionst
ri
ng.
Af
il
eis aut
omat
ical
l
ycr
eat
ed on t
he ser
veri
nthe desi
gnat
ed t
empor
ary
di
rect
oryandbear
sthenameoft
heuni
quei
dent
if
ierpr
efi
xedbysess_i
e
sess_
3c7f
oj34c3j
j
973hj
kop2f
c937e3443.
WhenaPHP scr
iptwant
stor
etr
iev
ethev
aluef
rom asessi
on v
ari
abl
e,PHP
aut
omat
ical
l
yget
stheuni
quesessi
oni
dent
if
ierst
ri
ngf
rom t
hePHPSESSI
Dcooki
eand
t
henl
ooksi
nit
stempor
arydi
rect
oryf
ort
hef
il
ebear
ingt
hatnameandav
ali
dat
ioncan
bedonebycompar
ingbot
hval
ues.
Asessi
onendswhent
heuserl
osest
hebr
owseroraf
terl
eav
ingt
hesi
te,
theser
ver
wi
l
lter
minat
ethesessi
onaf
terapr
edet
ermi
nedper
iodoft
ime,
commonl
y30mi
nut
es
dur
ati
on.
St
art
ingaPHPSessi
on
A PHP session i
s easil
y star
ted by making a cal
ltot he sessi
on_star
t()
funct
ion.
Thisfunct
ionfir
stchecksifasessionisal
readystar
tedandifnoneisstart
ed
thenitst
artsone.I
tisrecommendedt oputt hecal
ltosessi
on_st
art(
)atthebeginni
ng
ofthepage.
Sessi
onvar
iabl
esar estor
edi nassoci
ati
vearraycal
l
ed$_
SESSI
ON[
].These
v
ari
abl
escanbeaccesseddur
ingli
fet
imeofasession.
NehaMi
dha,
Lect
urer GPPanchkul
a
Thefol
l
owingexamplest
art
sasessi
onthenr
egist
eravari
abl
ecal
l
edcount
er
t
hati
sincr
ement
edeachtimethepagei
svi
sit
edduri
ngthesessi
on.
Makeuseofi
sset
()f
unct
iont
ochecki
fsessi
onv
ari
abl
eisal
readyset
ornot
.Putt
hiscodei
nat
est
.phpf
il
eandl
oadt
hisf
il
emanyt
imest
o
seet
her
esul
t−
<?
php
sessi
on_
star
t
()
;
i
f(isset($_SESSI
ON['
count
er'
])){
$_ SESSION['
count
er'
]+=1;
}
else{
$_ SESSION['
count
er'
]=1;
}
$msg="Youhavevisi
tedthi
spage"
.$_
SESSI
ON[
'
count
er'
]
;
$msg.
="inthi
ssession.
";
?
>
<ht
ml>
<head>
<tit
le>Set
ti
ngupaPHPsessi
on</
ti
tl
e>
</head>
<body>
<?phpecho($msg)
;?>
</body
>
</ht
ml>
Itwil
lpr
oducet
hef
oll
owi
ngr
esul
t−
Youhav
evi
sit
edt
hispage1i
nthi
ssessi
on.
Dest
roy
ingaPHPSessi
on
APHPsessioncanbedestr
oy edbysessi on_
destr
oy()f
uncti
on.Thisfunctiondoesnot
needanyargumentandasinglecallcandest r
oyal
lthesessi
onv ari
ables.Ifyouwant
todestr
oyasi ngl
esessi
onv ar
iabl
et heny oucanuseunset (
)f unct
iont ounseta
sessi
onvar
iabl
e.
Her
eist
heexampl
etounsetasi
ngl
evar
iabl
e−
<?php
unset(
$_SESSI
ON['
count
er'
]
);
?>
Hereisthecal
lwhi
chwill
destr
oyal
lthesessi
onv
ari
abl
es−
NehaMi
dha,
Lect
urer GPPanchkul
a
<?php
sessi
on_
dest
roy
();
?>
Turni
ngonAut
oSessi
on
Youdon'tneedtocal
lstart
_sessi
on()functi
ontostar
tasessionwhenauserv
isi
ts
yoursi
tei
fyoucansetsessi
on.aut
o_startvar
iabl
eto1i
nphp.
inifi
l
e.
Sessi
onswi
thoutcooki
es
Theremaybeacasewhenauserdoesnotal
l
owtostor
ecooki
esont
hei
rmachi
ne.So
ther
eisanot
hermet
hodtosendsessi
onI
Dtothebr
owser.
Alt
ernat
ivel
y,y
oucanuset heconstantSIDwhichisdef
inedifthesessi
onstart
ed.If
the cli
ent di
d not send an appr opri
ate sessi
on cookie, i
t has the form
sessi
on_name=sessi
on_i
d.Other
wise,itexpandstoanempt ystri
ng.Thus,youcan
embedituncondi
ti
onall
yint
oURLs.
Thef ol
l
owing exampledemonst
rat
eshow t
oregi
sterav
ari
abl
e,and how t
oli
nk
cor
rectl
ytoanotherpageusi
ngSI
D.
<?
php
sessi
on_
star
t
()
;
i
f(isset(
$_SESSION['
count
er'
]
)){
$_ SESSION[
'count
er'
]=1;
}
else{
$_ SESSION[
'count
er'
]++;
}
$msg="Youhavevisi
tedthi
spage"
.$_
SESSI
ON[
'
count
er'
]
;
$msg.
="inthi
ssession.
";
echo($msg)
;
?
>
<p>
Tocont
inuecl
i
ckf
oll
owi
ngl
i
nk<br/
>
<ahref="next
page.
php?<?
phpechoht
mlspeci
alchar
s(SI
D);
?>"
>
</p>
Itwil
lpr
oducethefol
l
owingresul
t−
Youhavevisi
tedt
hispage1inthi
s
sessi
on.Toconti
nuecli
ckfol
lowi
ng
l
ink
NehaMi
dha,
Lect
urer GPPanchkul
a
Ev
entmanagementi
nPHP
Ev
entManagementSy
stem i
sawebappl
i
cat
iondev
elopedwi
thPHPandMYSQL
t
omaket
heev
ent
smanagementef
fect
ive.Ev
entManagementi
snecessar
ybecause
t
hemor
epopul
arabr
andi
s,t
hel
esserr
eluct
anti
ndi
vi
dual
swi
l
lbef
orexper
iment
ing
wi
thnew i
temspr
opel
l
edbyt
hatbr
and.Occasi
ont
heexecut
ive’
sabi
l
iti
esar
e,i
nthi
s
way
,fundament
alf
ort
heor
gani
zat
iont
ogett
her
equi
redi
ntr
oduct
ionandassembl
ea
posi
ti
vepi
ctur
eoft
hegener
alor
gani
zat
ionj
ustasanybr
andspeci
fi
cal
l
y.Theydonot
j
ustf
il
li
nasanoppor
tuni
tyf
oranent
renchedor
gani
zat
iont
orecov
eri
tssi
gni
fi
canceby
pul
l
ingi
nanexpandi
ngnumberofi
mmi
nentcl
i
ent
syeti
naddi
ti
onempoweraspr
out
ing
or
gani
zat
iont
odev
elopaf
eel
i
ngofent
husi
asm f
ort
heev
ery
dayci
ti
zensaboutt
he
i
temsandadmi
nist
rat
ionst
heyof
fer
.
Usi
ng t
he sof
twar
e,we can per
for
m af
unct
ion r
elat
ed t
o whatev
ent
s
managementcompanyper
for
mst
hati
ncl
udescr
eat
ingev
ent
s,chooser
esour
cesand
l
ocat
ion,l
ogi
n and manage/
edi
t/
updat
e/del
ete ev
ent
sand l
ocat
ion l
i
st,make new
announcement
soft
hei
rcompanyandsoon.User
scansendanyi
nqui
ri
esusi
ngt
he
cont
actf
orm andt
hemessagei
sst
oredonadat
abaseandcanbeseent
her
eonl
y.
Feat
uresofEv
entManagementSof
twar
e
Thi
ssy
stem i
sbasedont
heev
entmanagementr
ecor
dsandt
oaddandedi
t.
TheGUIi
suser
-f
ri
endl
yandr
esponsi
ve.
LessTi
meConsumed.
Thesy
stem makeseasyt
ouset
omanaget
heev
ent
.
Cr
eat
enewEv
ent
sal
ongwi
thev
entname,
eventst
artdat
e,ev
entenddat
e,ev
ent
l
ocat
ion,
eventt
otal
cost
.
Seeal
ll
istofev
ent
scr
eat
edwi
tht
hei
rpr
operdet
ail
s.
Addnewl
ocat
ionsf
orev
ent
sthati
ncl
udesName,
addr
ess,
locat
ionmanagername,
number
,number
sofpeopl
ecapaci
tyandnumberoff
aci
l
iti
esi
ncl
udedi
nthe
l
ocat
ion.
Edi
t/
updat
eanddel
etel
ocat
ions.
Announceev
ent
sont
hewebsi
tepages
Logi
npr
otect
ionsy
stem
SendCont
actmessage
NehaMi
dha,
Lect
urer GPPanchkul
a
I
ntr
oduct
iont
ocont
entmanagementsy
stemsbasedonPHP
Cont
entdev
elopmenthasbecomeasensat
ionalmeansofshar
ingi
nfor
mat
ionov
er
t
hei
nter
net
.Ev
ent
henon-
techni
caluser
sgott
heabi
l
ityt
opubl
i
shcont
enteasi
l
yand
qui
ckl
yont
heWor
ldWi
deWeb.I
tisal
lpossi
blebecauseoft
heeasy
-useofcont
ent
managementt
ool
s av
ail
abl
e and i
s wi
del
y used by f
ir
ms,news or
gani
zat
ions,
educat
ionali
nst
it
uti
ons,andot
herbusi
nesses.I
nthi
schapt
er,y
ouwi
l
llear
naboutt
he
conceptofCMSandwhyi
tiswi
del
yaccept
edi
nthemar
ket
.
Whati
sCont
entManagementSy
stem?
Cont
entManagementSy
stem (
CMS)canbedef
inedasat
oolorsof
twar
epr
ogr
am
cont
aini
ngasetofi
nter
rel
atedpr
ogr
amsusedf
orcr
eat
ingandmanagi
ngdi
ff
erent
di
git
aloronl
i
necont
ent
.Somef
amousexampl
esofCMSsof
twar
ear
eJooml
a,Dr
upal
,
Wor
dPr
ess,
TYPO3,
etc.Thet
ypi
cal
useofCMSesar
eint
woar
eas:
1.Ent
erpr
iseCont
entManagement(
ECM)and
2.WebCont
entManagement(
WCM)
I
nthemaj
ori
tyoft
hecases,i
tcansuppor
tmanyuser
s,l
ett
ingt
hem wor
kint
he
associ
ati
on.Forexampl
e,Wor
dPr
essmakesi
tpossi
blet
ocr
eat
emanyadmi
nist
rat
ive
user
s,wher
eeachonehasdi
ff
erentpr
ivi
l
egeshencemaki
ngt
hewor
kpr
ogr
essi
n
par
all
el.Cont
entmanagementsy
stemsal
socompr
iseoft
extaswel
last
hel
ayoutand
desi
gnf
eat
uresl
i
ket
hef
aci
l
ityt
oupl
oadmul
ti
medi
acont
entl
i
kephot
os,v
ideos,audi
o,
maps,
orev
enanysour
cecode.
Component
sofCont
entManagementSy
stem
Acont
entmanagementsy
stem i
scomposedoft
womaj
orcomponent
s.Thesear
e:
Acont
entmanagementappl
i
cat
ion(
CMA)i
sagr
aphi
caluseri
nter
facet
hatal
l
ows
i
tsuser
stocr
eat
e,del
ete,modi
fy,andpubl
i
shcont
entev
enwi
thoutt
heknowl
edge
ofHTMLorot
herpr
ogr
ammi
ngl
anguagest
hatar
enecessar
ytocr
eat
ewebpages.
Acont
entdel
i
ver
yappl
i
cat
ion(
CDA)i
sresponsi
blef
ort
heback-
endser
vices.I
t
managesaswel
lasdel
i
ver
scont
entaf
terf
ramedi
ntheCMA.
NehaMi
dha,
Lect
urer GPPanchkul
a
Feat
uresofCont
entManagementSy
stem
UserManagement
:Thi
sper
mit
sthemanagementofuseri
nfor
mat
ionl
i
ket
her
oles
ofdi
ff
erentuser
sal
l
ott
edt
owor
ksi
mul
taneousl
y,suchascr
eat
ingordel
eti
ngt
he
user
,changet
heuser
name,
passwor
d,andot
herr
elat
edi
nfor
mat
ion.
ThemeSy
stem:Thi
sal
l
owsust
omodi
fyt
hesi
tev
iewaswel
lasf
unct
ional
i
tyusi
ng
st
ylesheet
s,i
mages,
andt
empl
ates.
Ext
endi
ngPl
ugi
ns:Di
ff
erentpl
ugi
nsar
eof
fer
ed,whi
chgi
vescust
om f
unct
ional
i
ties
andf
eat
urest
ocr
eat
etheCMSsi
te.
Sear
chEngi
neOpt
imi
zat
ion:I
tisembeddedwi
thal
otofsear
chengi
neopt
imi
zat
ion
(
SEO)t
ool
smaki
ngcont
entSEOmor
est
rai
ght
for
war
d.
Medi
aManagement
:isusedf
ormanagi
ngt
hemedi
afi
l
esandf
older
,wi
thupl
oadi
ng
medi
acont
ent
seasyandef
for
tl
ess.
Mul
ti
li
ngual
:Tr
ansl
ati
onoft
hel
anguage,aspr
efer
redbyt
heuser
,ispossi
blet
hrough
CMS.
Adv
ant
agesofCont
entManagementSy
stem
Mostoft
heCMSi
sopensour
ceandi
sav
ail
abl
eforf
ree.
Easyandqui
ckupl
oadi
ngofmedi
afi
l
escanbedone.
Sever
alSEOt
ool
smakeon-
sit
eSEOsi
mpl
er.
Easycust
omi
zat
ioni
spossi
bleaspert
heneedoft
heuser
.
I
tcanmodi
fyCSSf
il
esaspert
hedesi
gnneededbyt
heuser
.
Manyt
empl
atesandpl
ugi
nsar
eav
ail
abl
eforf
ree.Cust
omi
zat
ionofpl
ugi
nsi
sal
so
possi
ble.
Cont
entedi
ti
ngi
sal
somor
ecomf
ort
abl
easi
tusest
heWYSI
WYGedi
tor
.
Di
sadv
ant
agesofCont
entManagementSy
stem
CMSsof
twar
eneedsat
ime-
to-
ti
meupdat
e,andhencet
heuserneedst
olookout
f
oranupdat
edv
ersi
on.
NehaMi
dha,
Lect
urer GPPanchkul
a
Theuseofdi
ff
erentpl
ugi
nscanmakey
ourwebsi
teheav
yandchal
l
engi
ngt
orun.
CMShost
ingi
squi
teexpansi
ve.
PHPknowl
edgei
srequi
redt
omodi
fyorchanget
heWor
dPr
esswebsi
te.
NehaMi
dha,
Lect
urer GPPanchkul
a
Chapt
er3
PHPandMy
SQL
Dat
abase
Adat
abasei
sasepar
ateappl
i
cat
iont
hatst
oresacol
l
ect
ionofdat
a.Eachdat
abase
has one ormor
e di
sti
nctAPI
sforcr
eat
ing,accessi
ng,managi
ng,sear
chi
ng and
r
epl
i
cat
ingt
hedat
aithol
ds.
Ot
herki
ndsofdat
ast
orescanal
sobeused,
suchasf
il
esont
hef
il
esy
stem orl
arge
hasht
abl
esi
nmemor
ybutdat
afet
chi
ngandwr
it
ingwoul
dnotbesof
astandeasywi
th
t
hoset
ypeofsy
stems.
Nowaday
s,weuser
elat
ional
dat
abasemanagementsy
stems(
RDBMS)t
ost
oreand
managehugev
olumeofdat
a.Thi
siscal
l
edr
elat
ionaldat
abasebecauseal
lthedat
ais
st
oredi
ntodi
ff
erentt
abl
esandr
elat
ionsar
eest
abl
i
shedusi
ngpr
imar
ykey
sorot
her
key
sknownasFor
eignKey
s.
ARel
ati
onal
Dat
aBaseManagementSy
stem (
RDBMS)i
sasof
twar
ethat−
Enablesyoutoi
mplementadatabasewitht
ables,col
umnsandi ndexes.
Guaranteest
heRefer
ent
ial
Int
egrit
ybetweenrowsofv ari
oustables.
Updatesthei
ndexesaut
omati
cally
.
I
nterpret
sanSQLqueryandcombi nesi
nfor
mat i
onfrom var
ioustabl
es.
RDBMSTer
minol
ogy
Befor
ewepr oceedtoexpl
aintheMy
SQLdat
abasesy
stem,l
etusr
evi
seaf
ew
def
ini
ti
onsrel
atedtothedat
abase.
My
SQLDat
abase
My
SQLi
saf
ast
,easy
-to-
useRDBMSbei
ngusedf
ormanysmal
landbi
gbusi
nesses.
My
SQLi
sdev
eloped,mar
ket
ed and suppor
ted byMy
SQLAB,whi
chi
saSwedi
sh
company
.My
SQLi
sbecomi
ngsopopul
arbecauseofmanygoodr
easons−
My
SQLi
srel
easedunderanopen-
sour
cel
i
cense.Soy
ouhav
enot
hingt
opay
t
ousei
t.
My
SQLi
sav
erypower
ful
progr
am i
nit
sownr
ight
.Ithandl
esal
argesubset
oft
hef
unct
ional
i
tyoft
hemostex
pensi
veandpower
ful
dat
abasepackages.
My
SQLusesast
andar
dfor
m oft
hewel
l
-knownSQLdat
alanguage.
My
SQLwor
ksonmanyoper
ati
ngsy
stemsandwi
thmanyl
anguages
i
ncl
udi
ngPHP,
PERL,
C,C++,
JAVA,
etc.
My
SQLwor
ksv
eryqui
ckl
yandwor
kswel
lev
enwi
thl
argedat
aset
s.
My
SQLi
sver
yfr
iendl
yto PHP,t
hemostappr
eci
atedl
anguagef
orweb
dev
elopment
.
My
SQLsuppor
tsl
argedat
abases,upt
o50mi
l
li
onr
owsormor
einat
abl
e.
Thedef
aul
tfi
l
esi
zel
i
mitf
orat
abl
eis4GB,
buty
oucani
ncr
easet
his(
ify
our
oper
ati
ngsy
stem canhandl
eit
)toat
heor
eti
call
i
mitof8mi
l
li
ont
eraby
tes
(
TB)
.
My
SQLi
scust
omi
zabl
e.Theopen-
sour
ceGPLl
i
censeal
l
owspr
ogr
ammer
s
t
omodi
fyt
heMy
SQLsof
twar
etof
itt
hei
rownspeci
fi
cenv
ironment
s.
Connect
ingt
oMy
SQL
NehaMi
dha,
Lect
urer GPPanchkul
a
I
nor
dert
ost
oreoraccesst
hedat
ainsi
deaMy
SQLdat
abase,y
ouf
ir
stneedt
o
connectt
otheMy
SQLdat
abaseser
ver
.PHPof
fer
stwodi
ff
erentway
stoconnectt
o
My
SQLser
ver
:My
SQLi
(Impr
ovedMy
SQL)andPDO(
PHPDat
aObj
ect
s)ext
ensi
ons.
Whi
l
ethe PDO ext
ensi
on i
s mor
e por
tabl
e and suppor
tsmor
ethan t
wel
ve
di
ff
erentdat
abases,My
SQLiex
tensi
on as t
he name suggest
s suppor
ts My
SQL
dat
abaseonl
y.My
SQLiext
ensi
onhowev
erpr
ovi
desaneasi
erwayt
oconnectt
o,and
execut
equer
ieson,aMy
SQLdat
abaseser
ver
.Bot
hPDOandMy
SQLiof
feranobj
ect
-
or
ient
edAPI
,butMy
SQLial
soof
fer
sapr
ocedur
alAPIwhi
chi
srel
ati
vel
yeasyf
or
begi
nner
stounder
stand.
I
n PHP y
ou can easi
l
y do t
his usi
ng t
he my
sql
i
_connect
()f
unct
ion.Al
l
communi
cat
ionbet
weenPHPandt
heMy
SQLdat
abaseser
vert
akespl
acet
hrought
his
connect
ion.Her
e'r
ethebasi
csy
ntaxesf
orconnect
ingt
oMy
SQLusi
ngMy
SQLiandPDO
ext
ensi
ons:
Sy
ntax:
MySQLi
,Pr
ocedur
alway
$l
i
nk=my
sql
i
_connect
("
host
name"
,"user
name"
,"passwor
d",
"dat
abase"
);
Sy
ntax:
MySQLi
,Obj
ectOr
ient
edway
$my
sql
i=newmy
sql
i
("host
name"
,"user
name"
,"passwor
d",
"dat
abase"
);
The host
name par
amet
eri
nthe abov
e sy
ntaxspeci
fyt
he hostname (
e.g.
l
ocal
host
),orI
Paddr
essoft
heMy
SQLser
ver
,wher
east
heuser
nameandpasswor
d
par
amet
ers speci
fi
es t
he cr
edent
ial
sto access My
SQL ser
ver
,and t
he dat
abase
par
amet
er,i
fpr
ovi
ded wi
l
lspeci
fyt
hedef
aul
tMy
SQL dat
abaset
o beused when
per
for
mingquer
ies.
<?
php
$l
i
nk=my
sql
i
_connect
("
local
host
","
root
","
")
;
/
/Check
connect
ion
i
f(
$li
nk===f
alse)
NehaMi
dha,
Lect
urer GPPanchkul
a
{
NehaMi
dha,
Lect
urer GPPanchkul
a
di
e("
ERROR:
Coul
dnotconnect
.".my
sql
i
_connect
_er
ror
())
;
/
/Pr
inthosti
nfor
mat
ion
echo"
ConnectSuccessf
ull
y.Hosti
nfo:
".my
sql
i
_get
_host
_inf
o($l
i
nk)
;
?
>
Cl
osi
ngt
heMy
SQLDat
abaseSer
verConnect
ion
Theconnect
iont
otheMy
SQLdat
abaseser
verwi
l
lbecl
osedaut
omat
ical
l
y
assoonast
heexecut
ionoft
hescr
iptends.Howev
er,i
fyouwantt
ocl
osei
t
ear
li
ery
oucandot
hisbysi
mpl
ycal
l
ingt
hePHPmy
sql
i
_cl
ose(
)funct
ion.
<?
php
$l
i
nk=my
sql
i
_connect
("
local
host
","
root
","
")
;
/
/Check
connect
ion
i
f(
$li
nk===f
alse)
di
e("
ERROR:
Coul
dnotconnect
.".my
sql
i
_connect
_er
ror
())
;
/
/Pr
inthosti
nfor
mat
ion
echo"
ConnectSuccessf
ull
y.Hosti
nfo:
".my
sql
i
_get
_host
_inf
o($l
i
nk)
;
/
/
Cl
oseconnect
ionmy
sql
i
_cl
ose(
$li
nk)
;
?
>
Cr
eat
eaMy
SQLDat
abase
TheCREATEDATABASEst
atementi
susedt
ocr
eat
eadat
abasei
nMy
SQL.
NehaMi
dha,
Lect
urer GPPanchkul
a
Thef
oll
owi
ngexampl
escr
eat
eadat
abasenamed"
myDB"
:
<?
php
$ser
ver
name="
local
host
";
$user
name="
user
name"
;
$passwor
d="
passwor
d";
/
/Cr
eat
econnect
ion
$conn=my
sql
i
_connect
($ser
ver
name,
$user
name,
$passwor
d);
/
/Check
connect
ioni
f
(
!$conn){
di
e("
Connect
ionf
ail
ed:
".my
sql
i
_connect
_er
ror
())
;
/
/Cr
eat
edat
abase
$sql
="CREATEDATABASEmy
DB"
;
i
f(my
sql
i
_quer
y($conn,
$sql
)){
echo"
Dat
abasecr
eat
edsuccessf
ull
y";
}el
se{
echo"
Err
orcr
eat
ingdat
abase:
".my
sql
i
_er
ror
($conn)
;
my
sql
i
_cl
ose(
$conn)
;
?
>
Cr
eat
eaMy
SQLTabl
e
TheCREATETABLEst
atementi
susedt
ocr
eat
eat
abl
einMy
SQL.
CREATETABLEMy
Guest
s(
NehaMi
dha,
Lect
urer GPPanchkul
a
i
dINT(
6)UNSI
GNEDAUTO_
INCREMENTPRI
MARYKEY,
f
ir
stnameVARCHAR(
30)NOTNULL,
l
ast
nameVARCHAR(
30)NOTNULL,
emai
lVARCHAR(
50)
,
reg_
dat
e TI MESTAMP DEFAULT CURRENT_
TIMESTAMP ON UPDATE
CURRENT_
TIMESTAMP
Thef
oll
owi
ngexampl
eshowshowt
ocr
eat
ethet
abl
einPHP:
<?
php
$ser
ver
name="
local
host
";
$user
name="
user
name"
;
$passwor
d="
passwor
d";
$dbname="
myDB"
;
/
/Cr
eat
econnect
ion
$conn=my
sql
i
_connect
($ser
ver
name,
$user
name,
$passwor
d,$dbname)
;
/
/Check
connect
ioni
f
(
!$conn){
di
e("
Connect
ionf
ail
ed:
".my
sql
i
_connect
_er
ror
())
;
/
/sql
tocr
eat
etabl
e
$sql
="CREATETABLEMy
Guest
s(
i
dINT(
6)UNSI
GNEDAUTO_
INCREMENTPRI
MARYKEY,
f
ir
stnameVARCHAR(
30)NOTNULL,
l
ast
nameVARCHAR(
30)NOTNULL,
emai
lVARCHAR(
50)
,
NehaMi
dha,
Lect
urer GPPanchkul
a
reg_
dat
e TIMESTAMP DEFAULT CURRENT_
TIMESTAMP ON UPDATE
CURRENT_
TIMESTAMP
)
";
i
f(my
sql
i
_quer
y($conn,
$sql
)){
echo"
Tabl
eMy
Guest
scr
eat
edsuccessf
ull
y";
el
se
echo"
Err
orcr
eat
ingt
abl
e:".my
sql
i
_er
ror
($conn)
;
my
sql
i
_cl
ose(
$conn)
;
?
>
I
nser
tDat
aInt
oMy
SQL
Af
teradat
abaseandat
abl
ehav
ebeencr
eat
ed,
wecanst
artaddi
ngdat
a
i
nthem.Her
ear
esomesy
ntaxr
ulest
ofol
l
ow:
TheSQLquerymustbequotedinPHP
Str
ingv
aluesinsi
det
heSQLquer ymustbequot
ed
Numeri
cv al
uesmustnotbequoted
ThewordNULLmustnotbequot ed
TheI
NSERTI
NTOst
atementi
susedt
oaddnewr
ecor
dst
oaMy
SQLt
abl
e:
I
NSERTINTOt
abl
e_name(
col
umn1,
col
umn2,
col
umn3,
.
..
)VALUES(
val
ue1,
val
ue2,
v
alue3,
.
..
)
Thef
oll
owi
ngexampl
eshowsaddanewr
ecor
dtot
he"
MyGuest
s"t
abl
e:
<?
php
$ser
ver
name="
local
host
";
$user
name="
user
name"
;
NehaMi
dha,
Lect
urer GPPanchkul
a
$passwor
d="
passwor
d";
$dbname="
myDB"
;
/
/Cr
eat
econnect
ion
$conn=my
sql
i
_connect
($ser
ver
name,
$user
name,
$passwor
d,$dbname)
;
/
/Check
connect
ioni
f
(
!$conn){
di
e("
Connect
ionf
ail
ed:
".my
sql
i
_connect
_er
ror
())
;
$sql
="I
NSERTI
NTOMy
Guest
s(f
ir
stname,
last
name,
emai
l
)
VALUES(
'John'
,'
Doe'
,'
john@exampl
e.com'
)"
;
i
f(my
sql
i
_quer
y($conn,
$sql
)){
echo"
Newr
ecor
dcr
eat
edsuccessf
ull
y";
}el
se{
echo"
Err
or:
".$sql
."<br
>".my
sql
i
_er
ror
($conn)
;
my
sql
i
_cl
ose(
$conn)
;
?
>
Del
eteDat
aFr
om aMy
SQL
TheDELETEst
atementi
susedt
odel
eter
ecor
dsf
rom a
t
abl
e:DELETEFROM t
abl
e_name
NehaMi
dha,
Lect
urer GPPanchkul
a
WHEREsome_
col
umn=
some_
val
ueLet
'sl
ookatt
he
"
MyGuest
s"t
abl
e:
<?
php
$ser
ver
name="
local
host
";
$user
name="
user
name"
;
$passwor
d="
passwor
d";
$dbname="
myDB"
;
/
/Cr
eat
econnect
ion
$conn=my
sql
i
_connect
($ser
ver
name,
$user
name,
$passwor
d,$dbname)
;
/
/Check
connect
ioni
f
(
!$conn){
di
e("
Connect
ionf
ail
ed:
".my
sql
i
_connect
_er
ror
())
;
/
/sql
todel
etear
ecor
d
$sql
="DELETEFROM My
Guest
sWHEREi
d=3"
;
i
f(my
sql
i
_quer
y($conn,
$sql
))
{echo"
Recor
ddel
eted
successf
ull
y";
}el
se{
echo"
Err
ordel
eti
ngr
ecor
d:".my
sql
i
_er
ror
($conn)
;
}
NehaMi
dha,
Lect
urer GPPanchkul
a
my
sql
i
_cl
ose(
$conn)
;
NehaMi
dha,
Lect
urer GPPanchkul
a
?
>
Sel
ectDat
aFr
om aMy
SQL
TheSELECTst
atementi
susedt
osel
ectdat
afr
om oneormor
etabl
es:
SELECTcol
umn_
name(
s)FROM t
abl
e_name
orwecanuset
he*char
act
ert
osel
ectALLcol
umnsf
rom at
abl
e:
SELECT*FROM t
abl
e_name
Thef
oll
owingexampl
eselect
sthei
d,f
ir
stnameandl
ast
namecol
umnsf
rom t
he
MyGuest
stabl
eanddispl
aysitont
hepage:
<?
php
$ser
ver
name="
local
host
";
$user
name="
user
name"
;
$passwor
d="
passwor
d";
$dbname="
myDB"
;
/
/Cr
eat
econnect
ion
$conn=my
sql
i
_connect
($ser
ver
name,
$user
name,
$passwor
d,$dbname)
;
/
/Check
connect
ioni
f
(
!$conn){
di
e("
Connect
ionf
ail
ed:
".my
sql
i
_connect
_er
ror
())
;
$sql
="SELECTi
d,f
ir
stname,
last
nameFROM My
Guest
s";
$r
esul
t=my
sql
i
_quer
y($conn,
$sql
);
NehaMi
dha,
Lect
urer GPPanchkul
a
i
f(my
sql
i
_num_
rows(
$resul
t)>0){
/
/out
putdat
aofeachr
ow
whi
l
e($r
ow=my
sql
i
_fet
ch_
assoc(
$resul
t)
){
echo"
id:
".$r
ow[
"i
d"]
."-Name:
".$r
ow[
"f
ir
stname"
]."".$r
ow[
"l
ast
name"
]."
<br
>";
}el
se{
echo"
0resul
ts"
;
my
sql
i
_cl
ose(
$conn)
;
?
>