Web Module4
Web Module4
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
MODULE4
Chapt
er1:
PHPAr
ray
sandSuper
global
s
1. Ar
ray
s
2. $_
GETand$_
POSTSuper
global
Arr
ays
3. $_
SERVERAr
ray
4. $_
Fil
esAr
ray
5. Readi
ng/
Wri
ti
ngFi
l
es
Ar
ray
s
Anar
rayi
sadat
ast
ruct
uret
hatal
l
owst
hepr
ogr
ammert
ocol
l
ectanumberof
r
elat
edel
ement
stoget
heri
nasi
ngl
evar
iabl
e.
Unl
i
kemostot
herpr
ogr
ammi
ngl
anguages,
inPHPanar
rayi
sact
ual
l
yanor
der
ed
map,
whi
chassoci
ateseachv
aluei
nthear
raywi
thakey
.
Thi
sfl
exi
bil
i
tyal
l
owsust
ousear
ray
sinPHPi
namannersi
mil
art
oot
her
l
anguages’
arr
ays,
butwecanal
souset
hem l
i
keot
herl
anguages’
col
l
ect
ion
cl
asses.
Fi
gur
e1.
1:Vi
sual
i
zat
ionofakey
-val
uear
ray
Ar
raykey
s
o I
nmostpr
ogr
ammi
ngl
anguagesar
raykey
sar
eli
mit
edt
oint
eger
s,i
.e.
,
st
artat0,
andgoupby1.
o I
nPHP, smustbeei
key theri
nteger
sorst
ri
ngsandneednotbesequent
ial
.
o Thi
smeanswecannotuseanar
rayorobj
ectasakey(
doi
ngsowi
l
l
gener
ateaner
ror
).
o Oneshoul
dbeespeci
all
ycar
eful
aboutmi
xi
ngt
het
ypesoft
hekey
sforan
ar
raysi
ncePHPper
for
mscastoper
ati
onsont
hekey
sthatar
enot
i
nteger
sorst
ri
ngs.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page1
Kolar
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Arrayv
alues
o Unl
i
kekey
s,ar
enotr
est
ri
ctedt
oint
eger
sandst
ri
ngs.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page2
Kolar
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
o Theycanbeanyobj
ect
,ty
pe,
orpr
imi
ti
vesuppor
tedi
nPHP.
o Wecanev
enhav
eobj
ect
sofourownt
ypes,
sol
ongast
hekey
sint
he
ar
rayar
eint
eger
sandst
ri
ngs.
Def
ini
ngandAccessi
nganArray
Letusbeginbyconsi
deri
ngt
hesi
mpl
estar
ray
,whi
chassoci
ateseachv
alue
i
nsi
deofi
twi
thani
ntegeri
ndex(
star
ti
ngat0)
.
Thef
oll
owi
ngdecl
aresanempt
yar
raynamedday
s:
Todef
inet
hecont
ent
sofanar
rayasst
ri
ngsf
ort
heday
soft
heweekasshown
i
nFi
gur
e1.
1,wedecl
arei
twi
thacomma-
del
i
mit
edl
i
stofv
aluesi
nsi
det
he
()
bracesusi
ngei
theroft
wof
oll
owi
ngsy
ntaxes:
I
ntheabov
eexampl
e,becausenokey
sar
eexpl
i
cit
lydef
inedf
ort
hear
ray
,the
def
aul
tkeyv
aluesar
e0,
1,2,
...,
n.
Not
icet
hatwedonothav
etopr
ovi
deasi
zef
ort
hear
ray
:ar
ray
sar
edy
nami
cal
l
y
si
zedasel
ement
sar
eaddedt
othem.
El
ement
swi
thi
naPHPar
rayar
eaccessedi
namannersi
mil
art
oot
her
pr
ogr
ammi
ngl
anguages,
thati
s,usi
ngt
hef
ami
l
iarsquar
ebr
acketnot
ati
on.
Thecodeexampl
ebel
ow echoest
hev
alueofour$day
sar
rayf
ort
hekey
=1,whi
ch
r
esul
tsi
nout
putofTue.
Wecoul
dal
sodef
inet
hear
rayel
ement
sindi
vi
dual
l
yusi
ngt
hesamesquar
e
br
acketnot
ati
on:
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page3
Kolar
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
I
nPHP,
wear
eal
soabl
etoexpl
i
cit
lydef
inet
hekey
sinaddi
ti
ont
othev
alues.
Thi
sal
l
owsust
ousekey
sot
hert
hant
hecl
assi
c0,
1,2,
..,
ntodef
inet
he
i
ndexesofanar
ray
.
Fi
gur
e1.
2:Assi
gni
ngkey
stov
alues(
lef
t)andAr
raywi
thst
ri
ngsaskey
sandi
nteger
sasv
alues(
lef
t)
Expl
i
citcont
rol
oft
hekey
sandv
aluesopenst
hedoort
okey
sthatdonotst
artat
0,
arenotsequent
ial
,andt
hatar
enotev
eni
nteger
s(butr
atherst
ri
ngs)
.
Thi
siswhywecanal
soconsi
deranar
rayt
obeadi
cti
onar
yorhashmap.
Theset
ypesofar
ray
sinPHPar
egener
all
yref
err
edt
oasassoci
ati
vear
ray
s.
Key
smustbeei
theri
ntegerorst
ri
ngv
alues,
butt
hev
aluescanbeanyt
ypeof
PHPdat
aty
pe,
incl
udi
ngot
herar
ray
s.e
Toaccessanel
ementi
nanassoci
ati
vear
ray
,wesi
mpl
yuset
hekeyv
aluer
ather
t
hanani
ndex.
Mul
ti
dimensi
onal
Arr
ays
PHPalsosuppor
tsmul
ti
dimensi
onal
arr
ays.
Thev
aluesf
oranar
raycanbeanyPHPobj
ect
,whi
chi
ncl
udesot
herar
ray
s.
Fi
gur
e1.
3:I
ll
ust
rat
est
hest
ruct
ureoft
heset
womul
ti
dimensi
onal
arr
ays.
I
ter
ati
ngt
hroughanAr
ray
Oneofthemostcommonpr
ogr
ammi
ngt
askst
hatwewi
l
lper
for
m wi
than
ar
rayi
stoi
ter
atet
hroughi
tscont
ent
s.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page4
Kolar
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Fi
gur
e1.
4:Vi
sual
i
zingmul
ti
dimensi
onal
arr
ays
Li
sti
ng1.
1il
l
ust
rat
eshowt
oit
erat
eandout
putt
hecont
entoft
he$day
sar
ray
usi
ngt
hebui
l
t-i
nfunct
ioncount
()al
ongwi
thexampl
esusi
ngwhi
l
e,dowhi
l
e,
andf
orl
oops.
Li
sti
ng1.
1:I
ter
ati
nganarr
ayusi
ngwhi
l
e, Li
sti
ng1.
2:I
ter
ati
ngt
hroughanassoci
ati
ve
ar
raydowhi
le,
andforl
oops usi
ngaf
oreachl
oop
The chal
l
enge ofusi
ng t
he cl
assi
cloop st
ruct
ures i
sthatwhen we hav
e
nonsequent
iali
ntegerkey
s(i
.e.
,anassoci
ati
vear
ray
),wecan’
twr
it
easi
mpl
e
l
oopt
hatusest
he$i
++const
ruct
.
To ov
ercome t
his chal
l
enge we need t
o use i
ter
atort
o mov
ethr
ough an
associ
ati
vear
rayasshowni
nLi
sti
ng1.
2.
AddingandDel
eti
ngElements
InPHP,ar
raysaredy
namic,
i.
e.,
theycangr
oworshr
inki
nsi
ze.
Anel
ementcanbeaddedt
oanar
raysi
mpl
ybyusi
ngakey
/indext
hathasn’
t
beenused,
asshownbel
ow:
Si
ncet
her
eisnocur
rentv
aluef
orkey5,t
hear
raygr
owsbyone,wi
tht
henew
key
/val
uepai
raddedt
otheendofourar
ray
.
I
fthekeyhadav
alueal
ready
,thesamest
yleofassi
gnmentr
epl
acest
hev
alueatt
hat
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page5
Kolar
WebTechnologyandI
t’
s 18CS6
Appl
i
cations 3
key.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page6
Kolar
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Asanal
ter
nat
ivet
ospeci
fyi
ngt
hei
ndex
,anewel
ementcanbeaddedt
otheend
ofanyar
rayusi
ngt
hef
oll
owi
ngt
echni
que:
Theadv
ant
aget
othi
sappr
oachi
sthatwedon’
thav
etowor
ryaboutski
ppi
ngan
i
ndexkey
.PHPi
smor
ethanhappyt
oletus“
ski
p”ani
ndex,asshowni
nthe
f
oll
owi
ngexampl
e.
Out
putf
ort
heabov
esni
ppeti
s:
o Thati
s,t
her
eisnowa“
gap”i
nourar
rayt
hatwi
l
lcausepr
obl
emsi
fwet
ry
i
ter
ati
ngt
hroughi
tusi
ngt
het
echni
ques.
o I
fwet
ryr
efer
enci
ng$day
s[6]
,fori
nst
ance,
itwi
l
lret
urnaNULLv
alue,
whi
chi
saspeci
alPHPv
aluet
hatr
epr
esent
sav
ari
abl
ewi
thnov
alue.
Wecanal
socr
eat
e“gaps”byexpl
i
cit
lydel
eti
ngar
rayel
ement
susi
ngt
heunset
()
f
unct
ionasshowni
nLi
sti
ng1.
3.
Li
sti
ng1.
3:Del
eti
ngel
ement
s Li
sti
ng1.
4:nonsequent
ial
key
sandusageofi
sset
()
Checki
ngIfaValueExist
s:
o Sincear
raykeysneednotbesequent
ial
,andneednotbei
nteger
s,wemay
runi
nt oascenar
iowher
ewewantt
ochecki
fav
aluehasbeensetf
ora
part
icularkey
.
o Aswi
thundef
inednul
lvar
iabl
es,
val
uesf
orkey
sthatdonotexi
st
ar
eal
soundef
ined.
o Tochecki
fav
alueexi
stsf
orakey
,wecant
her
efor
euset
hei
sset
()
f
unct
ion,
whi
chr
etur
nst
ruei
fav
aluehasbeenset
,andf
alseot
her
wise.
o Li
sti
ng1.
4def
inesanar
raywi
thnoni
ntegeri
ndexes,
andshowst
her
esul
t
ofaski
ngi
sset
()onsev
eral
indexes.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page7
Kolar
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Ar
raySor
ti
ng
Therear
emanybui
l
t-i
nsor
tfunct
ions,
whi
chsor
tbykeyorbyv
alue.Tosor
tthe
$day
sar
raybyi
tsv
alueswewoul
dsi
mpl
yuse:
o Ast
hev
aluesar
eal
lst
ri
ngs,
ther
esul
ti
ngar
raywoul
dbe:
Howev
er,
theabov
esor
tlosest
heassoci
ati
onbet
weent
hev
aluesandt
hekey
s!
Abet
tersor
t,onet
hatwoul
dhav
ekeptkey
sandv
aluesassoci
atedt
oget
her
,is:
o Ther
esul
ti
ngar
rayi
nthi
scasei
s:
Mor
eArrayOper
ati
ons
I
naddi
ti
ontothepower
ful
sor
tfunct
ions,
ther
ear
eot
herconv
eni
entf
unct
ionsy
oucanuse
onar
ray
s.
ar
ray
_key
s($someAr
ray
):Thi
smet
hodr
etur
nsani
ndexedar
raywi
tht
hev
alues
bei
ngt
hekey
sof$someAr
ray
.
o Forexampl
e,pr
int
_r(
arr
ay_
val
ues(
$day
s))out
put
s
ar
ray
_val
ues(
$someAr
ray
):Thi
sfunct
ionr
etur
nsani
ndexedar
raywi
tht
he
v
aluesbei
ngt
hev
aluesof$someAr
ray
.
o Forexampl
e,pr
int
_r(
arr
ay_
val
ues(
$day
s))out
put
s
ar
ray
_rand(
$someAr
ray
,$num=1)
:Thi
sfunct
ionr
etur
nsasmanyr
andom key
sas
ar
erequest
ed.I
fweonl
ywantone,t
hekeyi
tsel
fisr
etur
ned;ot
her
wise,anar
ray
ofkey
sisr
etur
ned.
o Forexampl
e,pr
int
_r(
arr
ay_
rand(
$day
s,2)
)mi
ghtout
put
:
ar
ray
_rev
erse(
$someAr
ray
):Thi
smet
hodr
etur
ns$someAr
rayi
nrev
erseor
der
.
Thepassed$someAr
rayi
slef
tunt
ouched.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page8
Kolar
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
o Forexampl
e,pr
int
_r(
arr
ay_
rev
erse(
$day
s))out
put
s:
ar
ray
_wal
k($someAr
ray
,$cal
l
back,
$opt
ional
Par
am)
:Ital
l
owsust
ocal
lamet
hod
(
$cal
l
back)
,foreachv
aluei
n$someAr
ray
.The$cal
l
backf
unct
iont
ypi
cal
l
ytakes
t
wopar
amet
ers,
thev
aluef
ir
st,
andt
hekeysecond.
o Anexampl
ethatsi
mpl
ypr
int
sthev
alueofeachel
ementi
nthear
rayi
s
shownbel
ow.
i
n_ar
ray
($needl
e,$hay
stack)
:Thi
smet
hodl
etsust
osear
char
ray$hay
stackf
ora
v
alue(
$needl
e).I
tret
urnst
ruei
fiti
sfound,
andf
alseot
her
wise.
shuf
fl
e($someAr
ray
):Thi
smet
hodshuf
fl
es$someAr
ray
.Anyexi
sti
ngkey
sar
e
r
emov
edand$someAr
rayi
snowani
ndexedar
rayi
fitwasn’
tal
ready
.
Super
global
Arr
ays
PHPusesspeci
alpr
edef
inedassoci
ati
vear
ray
scal
l
edsuper
globalv
ari
abl
est
hat
al
l
ow t
hepr
ogr
ammert
oeasi
l
yaccessHTTPheader
s,quer
yst
ri
ngpar
amet
ers,
andot
hercommonl
yneededi
nfor
mat
ion.
Theyar
ecal
l
ed super
globalbecauset
hesear
ray
sar
eal
way
sinscopeand
al
way
sexi
st,
readyf
ort
hepr
ogr
ammert
oaccessormodi
fyt
hem wi
thouthav
ing
t
ouset
hegl
obal
key
wor
d.
Tabl
e1.
1:Super
global
var
iabl
es
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page9
Kolar
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
$_
GETand$_
POSTSuper
global
Arr
ays
The$_
GETand$_
POSTar
ray
sar
ethemosti
mpor
tantsuper
global
var
iabl
esi
n
PHPsi
ncet
heyal
l
owt
hepr
ogr
ammert
oaccessdat
asentbyt
hecl
i
enti
naquer
y
st
ri
ng.
AnHTMLf
orm(
oranHTMLl
i
nk)al
l
owsacl
i
entt
osenddat
atot
heser
ver
.
Thatdat
aisf
ormat
tedsucht
hateachv
aluei
sassoci
atedwi
thanamedef
inedi
nthe
for
m.
I
fthef
orm wassubmi
tt
edusi
nganHTTPGETr
equest
,thent
her
esul
ti
ngURL
wi
l
lcont
aint
hedat
aint
hequer
yst
ri
ng.
PHPwi
l
lpopul
atet
hesuper
global
$_GETar
rayusi
ngt
hecont
ent
soft
hisquer
y
st
ri
ngi
ntheURL.
Fi
gur
e1.
5:HTTPr
equestt
o$_
GETar
ray Fi
gur
e1.
6:HTTPr
equestt
o$_
POSTar
ray
I
fthef
orm wassentusi
ngHTTPPOST,
thent
hev
alueswoul
dnotbev
isi
blei
n
t
heURL,
butwi
l
lbesentt
hroughHTTPPOSTr
equestbody
.
Fr
om t
hePHPpr
ogr
ammer
’sper
spect
ive,
almostnot
hingchangesf
rom aGET
dat
apostexceptt
hatt
hosev
aluesandkey
sar
enowst
oredi
nthe$_
POSTar
ray
.
Thi
smechani
sm gr
eat
lysi
mpl
i
fiesaccessi
ngt
hedat
apost
edbyt
heuser
,si
nce
weneednotpar
set
hequer
yst
ri
ngort
hePOSTr
equestheader
s.
Det
ermi
ningI
fAnyDat
aSent
PHPcanuset
hesamef
il
etohandl
ebot
hthedi
spl
ayofaf
orm aswel
last
he
f
ormi
nput
.
o Forexampl
e,asi
ngl
efi
l
eisof
tenusedt
odi
spl
ayal
ogi
nfor
mtot
he
user
,andt
hatsamef
il
eal
sohandl
est
hepr
ocessi
ngoft
hesubmi
tt
ed
f
orm dat
a,asshowni
nFi
gur
e1.
7.
oI
nsuchcaseswemaywantt
oknow whet
heranyf
orm dat
awas
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 10
WebTechnol
ogyandIt
’s 18CS6
Appl
i
cati
ons 3
submi
tt
edatal
lusi
ngei
therPOSTorGET.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 11
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Fi
gur
e1.
7:For
m di
spl
ayandpr
ocessi
ngbyt
hesamePHPpage
AccessingFor
m Arr
ayDat
a
Someti
mesinHTMLf
ormsy
oumi
ghthav
emul
ti
plev
aluesassoci
atedwi
tha
si
ngl
ename;
Li
sti
ng1.
5pr
ovi
desanot
herexampl
e.Not
icet
hateachcheckboxhast
he
samenamev
alue(
name="
day
").
Li
sti
ng1.
5:HTMLt
hatenabl
esmul
ti
ple Li
sti
ng1.
6:PHPcodet
odi
spl
ayan
ar
rayv
aluesf
oronename ofcheckboxv
ari
abl
es
I
ftheusersel
ect
smor
ethanonedayandsubmi
tst
hef
orm,
the$_
GET[
'
day
']
val
uei
nthesuper
global
arr
aywi
l
lonl
ycont
aint
hel
astv
aluef
rom t
hel
i
stt
hat
wassel
ect
ed.
Toovercomet
hisl
i
mit
ati
on,
youmustchanget
heHTMLi
nthef
orm.I
n
par
ti
cular
,youwi
l
lhav
etochanget
henameat
tr
ibut
eforeachcheckboxf
rom
daytoday[
].
Li
sti
ng1.
6toechot
henumberofday
ssel
ect
edandt
hei
rval
ues.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 12
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Usi
ngQuer
ySt
ri
ngsi
nHyper
li
nks
WKT,f
ormi
nfor
mati
onpackagedi
naquer
yst
ri
ngi
str
anspor
tedt
othe
ser
veri
noneoft
wol
ocat
ionsdependi
ngonwhet
hert
hef
orm met
hodi
s
GETorPOST.
I
tisal
soi
mpor
tantt
oreal
i
zet
hatmaki
nguseofquer
yst
ri
ngsi
snot
l
i
mit
edt
oonl
ydat
aent
ryf
orms.
I
tispossi
blet
ocombi
nequer
yst
ri
ngswi
thanchort
ags..Anchort
ags
(
i.
e.,
hyper
li
nks)al
souset
heHTTPGETmet
hod
o EX:Ourdat
abasemayhav
ehundr
edsort
housandsofbooksi
nit
:
sur
elyi
twoul
dbet
oomuchwor
ktocr
eat
easepar
atepagef
or
eachbook!
o I
twoul
dmakeal
otmor
esenset
ohav
easi
ngl
eDi
spl
ayBookpage
t
hatr
ecei
vesasi
nputaquer
yst
ri
ngt
hatspeci
fi
eswhi
chbookt
o
di
spl
ay,
asshowni
nFi
gur
e1.
8.
Fi
gur
e1.
8:Sensi
bleappr
oacht
odi
spl
ayi
ngi
ndi
vi
dual
itemsusi
ngquer
yst
ri
ngs
Sani
ti
zi
ngQuerySt
ri
ngs
Thepr
ocessofchecki
nguseri
nputf
ori
ncor
rectormi
ssi
ng
i
nfor
mat
ioni
ssomet
imesr
efer
redt
oast
hepr
ocessofsani
ti
zi
ng
useri
nput
s.
Li
sti
ng1.
7:Si
mpl
esani
ti
zat
ionofquer
yst
ri
ngv
alues
Ourpr
ogr
am mustbeabl
etohandl
ethef
oll
owi
ngcasesf
orev
eryquer
y
st
ri
ngorf
ormv
alue.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 13
WebTechnol
ogyandIt’
s 18CS6
Appl
i
cati
ons 3
o Ifquer
yst
ri
ngpar
amet
erdoesn’
texi
st.
o I
fquer
yst
ri
ngpar
amet
erdoesn’
tcont
ainav
alue.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 14
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
o I
fquer
yst
ri
ngpar
amet
erv
aluei
sn’
tthecor
rectt
ype.
o I
fval
uei
srequi
redf
oradat
abasel
ookup,
butpr
ovi
dedv
alue
doesn’
texi
sti
nthedat
abaset
abl
e.
$_
SERVERAr
ray
The$_
SERVERassoci
ati
vear
raycont
ainsav
ari
etyofi
nfor
mat
ionasf
oll
ows.
o I
tcont
ainssomeoft
hei
nfor
mat
ioncont
ainedwi
thi
nHTTP
r
equestheader
ssentbyt
hecl
i
ent
.
oI
tal
socont
ainsmanyconf
igur
ati
onopt
ionsf
orPHPi
tsel
fas
showni
nfi
gur
e1.
9
Fi
gur
e1.
9:Rel
ati
onshi
pbet
weenr
equestheader
s,t
heser
ver
,andt
he$_
SERVER
ar
ray
Touset
he$_
SERVERar
ray
,wesi
mpl
yref
ert
other
elev
antcase-
sensi
ti
vekeyname:
I
tiswor
thnot
ingt
hatbecauset
heent
ri
esi
nthi
sar
rayar
ecr
eat
edbyt
he
webser
ver
,notev
erykeyl
i
stedi
nthePHPdocument
ati
onwi
l
lnecessar
il
ybe
av
ail
abl
e.
Acompl
etel
i
stofkey
scont
ainedwi
thi
nthi
sar
rayi
sli
stedi
ntheonl
i
nePHP
document
ati
on,
butwewi
l
lcov
ersomeoft
hecr
it
ical
onesher
e.
Ser
verI
nfor
mati
onKeys
SERVER_NAMEisakeyi
nthe$_
SERVERar
rayt
hatcont
ainst
henameoft
he
si
tet
hatwasr
equest
ed.I
fwear
erunni
ngmul
ti
plehost
sont
hesamecode
base,
thi
scanbeausef
ulpi
eceofi
nfor
mat
ion.
SERVER_
ADDRi
sacompl
ement
arykeyt
ell
i
ngust
heI
Poft
heser
ver
.Ei
ther
oft
hesekey
scanbeusedi
nacondi
ti
onalt
oout
putext
raHTMLt
oident
if
ya
dev
elopmentser
ver
.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 15
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
DOCUMENT_
ROOT t
ell
sust
hef
il
elocat
ionf
rom whi
chwear
ecur
rent
ly
r
unni
ngourscr
ipt
.
Si
ncewear
eof
tenmov
ingcodef
rom dev
elopmentt
opr
oduct
ion,
thi
skeycan
beusedt
ogr
eatef
fectt
ocr
eat
escr
ipt
sthatdonotr
elyonapar
ti
cul
ar
l
ocat
iont
oruncor
rect
ly.
Thi
skeycompl
ement
stheSCRI
PT_
NAMEkeyt
hati
dent
if
iest
heact
ualscr
ipt
bei
ngexecut
ed.
RequestHeaderI
nfor
mat
ionKeys
Recall
thatt
hewebser
verrespondst
oHTTPr
equest
s,andt
hateachr
equest
cont
ainsar
equestheader
.
Thesekey
spr
ovi
depr
ogr
ammat
icaccesst
othedat
aint
her
equestheader
.
TheREQUEST_
METHODkeyr
etur
nst
her
equestmet
hodt
hatwasusedt
o
accesst
hepage:
thati
s,GET,
HEAD,
POST,
PUT.
TheREMOTE_
ADDRkeyr
etur
nst
heI
Paddr
essoft
her
equest
or,
whi
chcanbe
ausef
ulv
aluet
ousei
nyourwebappl
i
cat
ions.
I
nreal
-wor
ldsi
test
heseI
Paddr
essesar
eof
tenst
oredt
opr
ovi
deanaudi
ttr
ail
ofwhi
chI
Pmadewhi
chr
equest
s,especi
all
yonsensi
ti
vemat
ter
sli
kef
inance
andper
sonal
inf
ormat
ion.
Li
sti
ng1.
8:Accessi
ngt
heuser
-agentst
ri
ngLi
sti
ng1.
9:Usi
ngt
heHTTP_
REFERER
headeri
ntheHTTPheader
s t
opr
ovi
decont
ext
-dependentout
put
Oneoft
hemostcommonl
yusedr
equestheader
sist
heuser
-agentheader
,
whi
chcont
ainst
heoper
ati
ngsy
stem andbr
owsert
hatt
hecl
i
enti
susi
ng.Thi
s
headerv
aluecanbeaccessedusi
ngt
hekeyHTTP_
USER_
AGENTasshowni
n
Li
sti
ng1.
8.
HTTP_REFERERi
sanespeci
all
yusef
ulheader
.It
sval
uecont
ainst
headdr
ess
oft
hepaget
hatr
efer
redust
othi
sone(
ifany
)thr
oughal
i
nk.I
tiscommonl
y
usedi
nanal
yti
cst
odet
ermi
newhi
chpagesar
eli
nki
ngt
ooursi
teasshowni
n
Li
sti
ng1.
9.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 16
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
$_
FILESAr
ray
The$_
FILESassoci
ati
vear
raycont
ainsi
temst
hathav
ebeenupl
oadedt
othe
cur
rentscr
ipt
.
The<i
nputt
ype="
fi
le"
>el
ementi
susedt
ocr
eat
etheuseri
nter
facef
orupl
oadi
ng
af
il
efr
om t
hecl
i
entt
otheser
ver
.Theuseri
nter
facei
sonl
yonepar
toft
he
upl
oadi
ngpr
ocess.
HTMLRequi
redf
orFil
eUploads
Toall
owuserstoupl
oadfil
es,
ther
ear
esomespeci
fi
cthi
ngsy
oumustdo:
Fi
rst
,wemustensur
ethatt
heHTMLf
orm usest
heHTTPPOSTmet
hod,
si
ncet
ransmi
tt
ingaf
il
ethr
ought
heURLi
snotpossi
ble.
Second,
wemustaddt
heenct
ype="
mul
ti
par
t/f
orm-
dat
a"at
tr
ibut
etot
heHTML
f
ormt
hati
sper
for
mingt
heupl
oadsot
hatt
heHTTPr
equestcansubmi
tmul
ti
ple
pi
ecesofdat
a.
Fi
nal
l
ywemusti
ncl
udeani
nputt
ypeoff
il
einourf
orm.Thi
swi
l
lshowupwi
tha
br
owsebut
tonbesi
dei
tsot
heusercansel
ectaf
il
efr
om t
hei
rcomput
ert
obe
upl
oaded.
Li
sti
ng1.
10:
HTMLf
oraf
ormt
hatal
l
owsanupl
oad
Handl
i
ngt
heFil
eUploadinPHP
Thecor
respondi
ngPHPfi
ler
esponsi
blef
orhandl
i
ngt
heupl
oadwi
l
lut
il
ize
t
hesuper
global
$_FI
LESar
ray
.
Thi
sar
raywi
l
lcont
ainakey
=val
uepai
rforeachf
il
eupl
oadedi
nthepost
.
o Thekeyforeachel
ementwi
llbet
henameat tr
ibut
efrom t
heHTMLfor
m,
o whi
lethevaluewi
ll
beanarr
aycontai
ningi
nformati
onaboutt
hefi
l
eas
wel
last
hef
il
eit
sel
f.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 17
WebTechnol
ogyandIt
’s 18CS6
Appl
i
cati
ons 3
o Thekeysinthatar
rayar
ethename,
type,
tmp_
name,
err
or,
andsi
ze.
Fi
gur
e1.
10:
Dat
afl
owf
rom HTMLfor
mthr
oughPOSTt
oPHP$_
FILES
ar
ray
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 18
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Fi
gur
e1.
10i
l
lust
rat
est
hepr
ocessofupl
oadi
ngaf
il
etot
heser
verandhowt
he
cor
respondi
ngupl
oadi
nfor
mat
ioni
scont
ainedi
nthe$_
FILESar
ray
.
Thev
aluesf
oreachoft
hekey
s,i
ngener
al,
aredescr
ibedbel
ow.
o Namei
sast
ri
ngcont
aini
ngt
hef
ullf
il
enameusedont
hecl
i
entmachi
ne,
i
ncl
udi
nganyf
il
eext
ensi
on.I
tdoesnoti
ncl
udet
hef
il
epat
hont
hecl
i
ent
’s
machi
ne.
o Ty
pedef
inest
heMI
MEt
ypeoft
hef
il
e.Thi
sval
uei
spr
ovi
dedbyt
hecl
i
ent
br
owserandi
sther
efor
enotar
eli
abl
efi
eld.
o t
mp_
namei
sthef
ullpat
htot
hel
ocat
ionony
ourser
verwher
ethef
il
eis
bei
ngt
empor
ari
l
yst
ored.Thef
il
ewi
l
lceaset
oexi
stupont
ermi
nat
ionof
t
hescr
ipt
,soi
tshoul
dbecopi
edt
oanot
herl
ocat
ioni
fst
oragei
srequi
red.
o er
rori
san i
ntegert
hatencodesmanypossi
ble er
ror
sand i
ssett
o
UPLOAD_
ERR_
OK(
int
egerv
alue0)i
fthef
il
ewasupl
oadedsuccessf
ull
y.
o si
zei
sani
ntegerr
epr
esent
ingt
hesi
zei
nby
tesoft
heupl
oadedf
il
e.
Checki
ngforErr
ors
Forever
yuploadedf
il
e,t
her
eisaner
rorv
alueassoci
atedwi
thi
tint
he$_
FILESar
ray
.
Theer
rorv
aluesar
especi
fi
edusi
ngconst
antv
alues,
whi
chr
esol
vet
oint
eger
s.
Thev
aluef
orasuccessf
ulupl
oadi
sUPLOAD_
ERR_
OK,
andshoul
dbel
ookedf
or
bef
orepr
oceedi
nganyf
urt
her
.
Thef
ull
li
stofer
ror
sispr
ovi
dedi
nTabl
e1.
2andshowst
hatt
her
ear
emany
causesf
orbadf
il
eupl
oads.
Tabl
e1.
2:Er
rorCodesi
nPHPf
or Li
sti
ng1.
11:
Checki
ngeach
f
il
eFi
l
eUpl
oad upl
oadedf
orer
ror
s
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 19
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Apr
operf
il
eupl
oadscr
iptwi
l
lther
efor
echeckeachupl
oadedf
il
ebychecki
ngt
he
v
ari
ouser
rorcodesasshowni
nli
sti
ng1.
11.
Fi
l
eSi
zeRest
ri
cti
ons
Somescr
ipt
sli
mitt
hef
il
esi
zeofeachupl
oad.
Ther
ear
emanyr
easonst
odoso,
andi
deal
l
ywewoul
dpr
eventt
hef
il
efr
om ev
en
bei
ngt
ransmi
tt
edi
nthef
ir
stpl
acei
fiti
stool
arge.
Ther
ear
ethr
eemai
nmechani
smsf
ormai
ntai
ningupl
oadedf
il
esi
zer
est
ri
cti
ons:
o v
iaHTMLi
nthei
nputf
orm,
o Thi
stechni
queal
l
owsy
ourphp.
ini
maxi
mum f
il
esi
zet
obe
l
arge,
whi
l
elet
ti
ngsomef
ormsov
err
idet
hatl
argel
i
mitwi
tha
smal
l
erone
Li
sti
ng1.
12:
Limi
ti
ngupl
oadf
il
esi
zev
iaHTML
o v
iaJav
aScr
ipti
nthei
nputf
orm,
and
o Asi
ntui
ti
veasi
tis,
thi
shi
ddenf
iel
dcaneasi
l
ybeov
err
iddenby
t
hecl
i
ent
,andi
sther
efor
eunaccept
abl
east
heonl
ymeansof
l
i
mit
ingsi
ze.
Li
sti
ng1.
13:
Limi
ti
ngupl
oadf
il
esi
zev
iaJav
aScr
ipt
o v
iaPHPcodi
ng.
o Thi
stechni
quecheckst
hef
il
esi
zeont
heser
verbysi
mpl
y
checki
ngt
hesi
zef
iel
dint
he$_
FILESar
ray
.
Li
sti
ng1.
14:
Limi
ti
ngupl
oadf
il
esi
zev
iaPHP
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 20
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Li
mit
ingt
heTy
peofFi
l
eUpl
oad
Ev
eni
ftheupl
oadwassuccessf
ulandt
hesi
zewaswi
thi
ntheappr
opr
iat
eli
mit
s,
wemayst
il
lhav
eapr
obl
em.
Whati
fwewant
edt
heusert
oupl
oadani
mageandt
heyupl
oadedaMi
crosof
t
Wor
ddocument
?Wemi
ghtal
sowantt
oli
mitt
heupl
oadedi
maget
ocer
tai
n
i
maget
ypes,
suchasj
pgandpng,
whi
l
edi
sal
l
owi
ngbmpandot
her
s.
Toaccompl
i
sht
hist
ypeofchecki
ngy
out
ypi
cal
l
yexami
net
hef
il
eext
ensi
onand
t
het
ypef
iel
dandal
sot
ocompar
ethet
ypet
oval
i
dimaget
ypes.
Li
sti
ng1.
15showssampl
ecodet
ocheckt
hef
il
eext
ensi
onofaf
il
e,andal
sot
o
compar
ethet
ypet
oval
i
dimaget
ypes.
Li
sti
ng1.
15:
PHPcodet
olookf
orv
ali
d Li
sti
ng1.
16:
Usi
ngmov
e_upl
oaded_
fil
e()
f
unct
ionmi
met
ypesandf
il
eext
ensi
ons
Mov
ingt
heFi
l
e
Wit
hal
lofourchecki
ngcompl
eted,
wemaynowf
inal
l
ywantt
omov
ethe
t
empor
aryf
il
etoaper
manentl
ocat
ionony
ourser
ver
.
Ty
pical
l
y,wemakeuseoft
hePHPf
unct
ionmov
e_upl
oaded_
fil
e()
,whi
cht
akesi
n
t
het
empor
aryf
il
elocat
ionandt
hef
il
e’
sfi
nal
dest
inat
ion.
Thi
sfunct
ionwi
l
lonl
ywor
kift
hesour
cef
il
eexi
stsandi
fthedest
inat
ionl
ocat
ion
i
swr
it
abl
ebyt
hewebser
ver(
Apache)
.
I
fther
eisapr
obl
em t
hef
unct
ionwi
l
lret
urnf
alse,
andawar
ningmaybeout
put
.
Li
sti
ng1.
16i
l
lust
rat
esasi
mpl
euseoft
hef
unct
ion.
Readi
ng/
Wri
ti
ngFi
l
es
Bef
oret
heageoft
heubi
qui
tousdat
abase,
sof
twar
erel
i
edonst
ori
ngand
accessi
ngdat
ainf
il
es.
I
nwebdev
elopment
,theabi
l
ityt
oreadandwr
it
etot
extf
il
esr
emai
nsan
i
mpor
tantt
echni
cal
compet
ency
.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 21
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Ev
eni
foursi
teusesadat
abasef
orst
ori
ngi
tsi
nfor
mat
ion,
thef
actt
hatt
hePHP
f
il
efunct
ionscanr
ead/
wri
tef
rom af
il
eorf
rom anext
ernalwebsi
te(
i.
e.,f
rom a
URL)meanst
hatf
il
esy
stem f
unct
ionsst
il
lhav
erel
evanceev
eni
ntheageof
dat
abase-
dri
venwebsi
tes.
Ther
ear
etwobasi
ctechni
quesf
orr
ead/
wri
ti
ngf
il
esi
nPHP:
o St
ream access.I
nthi
stechni
que,
ourcodewi
l
lreadj
ustasmal
lpor
ti
onof
t
hef
il
eatat
ime.Whi
l
ethi
sdoesr
equi
remor
ecar
efulpr
ogr
ammi
ng,i
tis
t
hemostmemor
y-ef
fi
cientappr
oachwhenr
eadi
ngv
eryl
argef
il
es.
o Al
l
-In-
Memor
yaccess.I
nthi
stechni
que,wecanr
eadt
heent
ir
efi
l
eint
o
memor
y(i
.e.
,int
oaPHPv
ari
abl
e).Whi
l
enotappr
opr
iat
eforl
argef
il
es,i
t
doesmakepr
ocessi
ngoft
hef
il
eext
remel
yeasy
.
St
ream access
I
ntheC-
sty
lef
il
eaccessy
ousepar
atet
heact
sofopeni
ng,r
eadi
ng,andcl
osi
nga
f
il
e.
Thef
unct
ionf
open(
)takesaf
il
elocat
ionorURLandaccessmodeaspar
amet
ers.
Someoft
hecommonmodesar
e“r
”forr
ead,
“rw”f
orr
eadandwr
it
e,and“
c,”
whi
chcr
eat
esanewf
il
eforwr
it
ing.
Oncet
hef
il
eisopened,
wecanr
eadf
rom i
tinsev
eral
way
s.
o Tor
eadasi
ngl
eli
ne,
uset
hef
get
s()f
unct
ion,
whi
chwi
l
lret
urnf
alsei
f
t
her
eisnomor
edat
a,
o Tor
eadanar
bit
rar
yamountofdat
a(t
ypi
cal
l
yforbi
nar
yfi
l
es)
,use
f
read(
)andf
orr
eadi
ngasi
ngl
echar
act
erusef
get
sc(
).
o Fi
nal
l
y,whenf
ini
shedpr
ocessi
ngt
hef
il
eyoumustcl
osei
tusi
ngf
close(
).
Li
sti
ng1.
17i
l
lust
rat
esascr
iptusi
ngf
open(
),f
get
s()
,andf
close(
)tor
eada
f
il
eandechoi
tout
.
Towr
it
edat
atoaf
il
e,wecanempl
oyt
hef
wri
te(
)funct
ioni
nmucht
hesame
wayasf
get
s()
,passi
ngt
hef
il
ehandl
eandt
hest
ri
ngt
owr
it
e.
Li
sti
ng9.
19Openi
ng,
readi
ngl
i
nes, Tabl
e1.
3:I
n-Memor
yFi
l
eFunct
ions
andcl
osi
ngaf
il
e
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 22
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Al
l
-In-
Memor
yaccess
Whi
l
ethepr
evi
ousappr
oacht
oreadi
ng/
wri
ti
ngf
il
esgi
vesy
oucompl
ete
cont
rol
,thepr
ogr
ammi
ngr
equi
resmor
ecar
eindeal
i
ngwi
tht
hest
reams,
fil
e
handl
es,
andot
herl
ow-
lev
eli
ssues.
Theal
ter
nat
ivesi
mpl
erappr
oachi
smucheasi
ert
o use,att
hecostof
r
eli
nqui
shi
ngf
ine-
grai
nedcont
rol
asshowni
ntabl
e1.
3.
Thef
il
e_get
_cont
ent
s()andf
il
e_put
_cont
ent
s()f
unct
ionsal
l
owy
out
oreador
wr
it
eanent
ir
efi
l
einonef
unct
ioncal
l
.Tor
eadanent
ir
efi
l
eint
oav
ari
abl
e
y
oucansi
mpl
yuse:
Thesef
unct
ionsar
eespeci
all
yconv
eni
entwhenusedi
nconj
unct
ionwi
th
PHP’
smanypower
ful
str
ing-
processi
ngf
unct
ions.
Chapt
er2:
PHPClassesand
Obj
ect
s
1. Obj
ect
-Or
ient
edOv
erv
iew
2. Cl
assesandObj
ect
sinPHP
3. Obj
ectOr
ient
edDesi
gn
Obj
ect
-Or
ient
edOv
erv
iew
Ter
minol
ogy
Thenot
ionofpr
ogr
ammi
ngwi
thobj
ect
sal
l
owst
hedev
elopert
othi
nkabout
ani
tem wi
thpar
ti
cul
arpr
oper
ti
es(
att
ri
but
es/
dat
amember
s)andmet
hods
(
funct
ions)
.
Thest
ruct
ureoft
heseobj
ect
sar
edef
inedbycl
asses,whi
chout
li
net
he
pr
oper
ti
esandmet
hodsl
i
keabl
uepr
int
.
Eachv
ari
abl
ecr
eat
edf
rom acl
assi
scal
l
edanobj
ectori
nst
ance,andeach
obj
ectmai
ntai
nsi
tsownsetofv
ari
abl
es,andbehav
esi
ndependent
lyf
rom
t
hecl
assoncecr
eat
ed.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 23
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Fi
gur
e2.
1il
l
ust
rat
est
hedi
ff
erencesbet
weenacl
ass,
whi
chdef
inesan
obj
ect
’spr
oper
ti
esandmet
hods,
andt
heobj
ect
sori
nst
ancesoft
hatcl
ass.
Fi
gur
e2.
1:Rel
ati
onshi
pbet
weenacl
assandi
tsobj
ect
s
TheUni
fi
edModel
i
ngLanguage
Thest
andar
ddi
agr
ammi
ngnot
ati
onf
orobj
ect
-or
ient
eddesi
gni
sUML
(
Uni
fi
edModel
i
ngLanguage)
.
UMLi
sasucci
nctsetofgr
aphi
calt
echni
quest
odescr
ibesof
twar
edesi
gn
whi
chgi
vest
hev
isual
i
zat
ion/r
epr
esent
ati
onofobj
ect
sandcl
asses.
Sev
eralt
ypesofUMLdi
agr
am ar
edef
ined.Cl
assdi
agr
amsandobj
ect
di
agr
ams,i
npar
ti
cul
ar,ar
eusef
ult
ouswhendescr
ibi
ngt
hepr
oper
ti
es,
met
hods,
andr
elat
ionshi
psbet
weencl
assesandobj
ect
s.
Ex:Toi
l
lust
rat
ecl
assesandobj
ect
sinUML,consi
dert
hear
ti
stwehav
e
l
ookedati
ntheAr
tCaseSt
udy
.Ev
eryar
ti
sthasaf
ir
stname,l
astname,
bi
rt
hdat
e,bi
rt
hci
ty,anddeat
hdat
e.Usi
ngobj
ect
swecanencapsul
ate
t
hosepr
oper
ti
est
oget
heri
ntoacl
assdef
ini
ti
onf
oranAr
ti
st.
Fi
gure2.2:
Relat
ionshi
pbet
weenacl
ass Fi
gur
e2.
3:Di
ff
erentl
evel
sofUML
det
ailandi
tsobj
ectsinUML
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 24
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Di
ff
erencesbet
weenSer
verandDeskt
opObj
ect
s
One i
mpor
tantdi
sti
nct
ion bet
ween web pr
ogr
ammi
ng and deskt
op appl
i
cat
ion
pr
ogr
ammi
ngi
sthatt
heobj
ect
syoucr
eat
e(nor
mal
l
y)onl
yexi
stunt
ilawebscr
ipti
s
t
ermi
nat
ed.
Whi
l
edeskt
opsof
twar
ecanl
oadanobj
ecti
ntomemor
yandmakeuseofi
tfor
sev
eraluseri
nter
act
ions,
aPHPobj
ecti
sloadedi
ntomemor
yonl
yfort
hel
i
feoft
hat
HTTPr
equest
.
Fi
gur
e10.
4showsani
l
lust
rat
ionoft
hel
i
fet
imesofobj
ect
sinmemor
ybet
weena
deskt
opandabr
owserappl
i
cat
ion.
Fort
hisr
eason,
wemustusecl
assesdi
ff
erent
lyt
hani
nthedeskt
opwor
ld,
sincet
he
obj
ectmustber
ecr
eat
edandl
oadedi
ntomemor
yforeachr
equestt
hatr
equi
resi
t.
Obj
ect
-or
ient
ed web appl
i
cat
ions can see si
gni
fi
cantper
for
mance degr
adat
ion
compar
edt
othei
rfunct
ional
count
erpar
tsi
fobj
ect
sar
enotut
il
izedcor
rect
ly.
Unl
i
keadeskt
op,t
her
ear
epot
ent
ial
l
ymanyt
housandsofuser
smaki
ngr
equest
sat
once,so notonl
yobj
ect
sar
edest
roy
ed uponr
espondi
ng t
o eachr
equest
,but
memor
ymustbeshar
edbet
weenmanysi
mul
taneousr
equest
s,eachofwhi
chmay
l
oadobj
ect
sint
omemor
y.
Fi
gur
e2.
4:Li
fet
imeofobj
ect
sinmemor
yinwebv
ersusdeskt
opappl
i
cat
ions
Cl
assesandObj
ect
sinPHP
Classesshoul
dbedef
inedi
nthei
rownf
il
essot
heycanbei
mpor
tedi
ntomul
ti
ple
scri
pts.
AnyPHPscr
iptcanmakeuseofanext
ernal
classbyusi
ngoneoft
hei
ncl
ude
st
atement
sorf
unct
ions,
thati
s,i
ncl
ude,
incl
ude_
once,
requi
re,
orr
equi
re_
once.
Onceacl
asshasbeendef
ined,
wecancr
eat
easmanyi
nst
ancesoft
hatobj
ectas
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 25
WebTechnol
ogyandIt
’s 18CS6
Appl
i
cati
ons 3
memorywil
lal
lowusi
ngt
henewkey
wor
d.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 26
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Def
ini
ngCl
asses
ThePHPsynt
axf
ordef
ini
ngacl
assusest
hecl
asskey
wor
dfol
l
owedby
t
hecl
assnameand{}br
aces.
Thepr
oper
ti
esandmet
hodsoft
hecl
assar
edef
inedwi
thi
nthebr
aces.
TheAr
ti
stcl
asswi
tht
hepr
oper
ti
esi
l
lust
rat
edi
nFi
gur
e2.
2isdef
ined
usi
ngPHPi
nLi
sti
ng2.
1.
Eachpr
oper
tyi
nthecl
assi
sdecl
aredusi
ngoneoft
hekey
wor
dspubl
i
c,
pr
otect
ed,
orpr
ivat
efol
l
owedbyt
hepr
oper
tyorv
ari
abl
ename.
Li
sti
ng2.
1:Ar
ti
stcl
ass Li
sti
ng2.
2Inst
ant
iat
ingt
woAr
ti
stobj
ect
sandset
ti
ngi
tsobj
ect
’spr
oper
ti
es
I
nst
ant
iat
ingObj
ect
s
I
t’
simpor
tantt
onot
ethatdef
ini
ngacl
assi
snott
hesameasusi
ngi
t.
Tomakeuseofacl
ass,
onemusti
nst
ant
iat
e(cr
eat
e)obj
ect
sfr
om i
ts
def
ini
ti
onusi
ngt
henewkey
wor
d.
Tocr
eat
etwonewi
nst
ancesoft
heAr
ti
stcl
asscal
l
ed$pi
cassoand$dal
i
,
wei
nst
ant
iat
etwonewobj
ect
susi
ngt
henewkey
wor
dasf
oll
ows:
Pr
oper
ti
es
Oncewehav
einst
ancesofanobj
ect
,wecanaccessandmodi
fyt
he
pr
oper
ti
esofeachonesepar
atel
yusi
ngt
hev
ari
abl
enameandanar
row
(
),
whi
chi
sconst
ruct
edf
rom t
hedashandgr
eat
ert
hansy
mbol
s.
Li
sti
ng2.
2Showscodet
hatdef
inest
het
woAr
ti
stobj
ect
sandt
henset
s
al
lthepr
oper
ti
esf
ort
he$pi
cassoobj
ect
.
Const
ruct
ors
Li
sti
ng2.
2takesmul
ti
plel
i
nesandev
eryl
i
neofcodei
ntr
oduces
pot
ent
ial
mai
ntai
nabi
l
itypr
obl
ems,
especi
all
ywhenwedef
inemor
e
ar
ti
sts.
I
nsi
deofacl
assdef
ini
ti
on,
weshoul
dther
efor
edef
ineconst
ruct
ors.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 27
WebTechnol
ogyandIt
’s 18CS6
Appl
i
cati
ons 3
InPHP,const
ruct
orsar
edef
inedasf
unct
ionswi
tht
hename const
ruct
().
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 28
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Li
sti
ng2.
3showsanupdat
edAr
ti
stcl
assdef
ini
ti
ont
hatnow
i
ncl
udesaconst
ruct
or.
I
nsi
deofacl
assy
oumustal
way
suset
he$t
hissy
ntaxt
oref
erenceal
l
pr
oper
ti
esandmet
hodsassoci
atedwi
tht
hispar
ti
cul
ari
nst
anceofacl
ass.
Li
sti
ng2.
3:Aconst
ruct
oraddedt
othecl
assdef
ini
ti
onLi
sti
ng2.
4:Met
hoddef
ini
ti
on
Thi
snewconst
ruct
orcant
henbeusedwheni
nst
ant
iat
ingasshownbel
ow
Met
hods
Obj
ect
sar
eusef
ulwhenwedef
inebehav
iororoper
ati
onst
hatt
heycanper
for
m.
I
nobj
ect
-or
ient
edl
i
ngot
heseoper
ati
onsar
ecal
l
edmet
hodsandar
e
l
i
kef
unct
ions,
exceptt
heyar
eassoci
atedwi
thacl
assasshowni
n
l
i
sti
ng2.
4.
Theydef
inet
het
askseachi
nst
anceofacl
asscanper
for
m andar
eusef
ul
si
ncet
heyassoci
atebehav
iorwi
thobj
ect
s.
Toout
putt
hear
ti
st,
youcanuset
her
efer
enceandmet
hodnameasf
oll
ows:
Updat
edUMLdi
agr
am i
sshownbel
ow.
Fi
gur
e2.
5:Updat
edclassdi
agr
am
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 29
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Vi
sibi
l
ity
Thevi
si
bil
i
tyofapr
oper
tyormet
hoddet
ermi
nest
heaccessi
bil
i
tyofa
cl
assmember(
i.
e.,
apr
oper
tyormet
hod)andcanbesett
opubl
i
c,pr
ivat
e,
orpr
otect
ed.
Fi
gur
e2.
6:Vi
sibi
l
ityofcl
assmember
s
Publ
i
c
o Thepubl
i
ckey
wor
dmeanst
hatt
hepr
oper
tyormet
hodi
s
accessi
blet
oanycodet
hathasar
efer
encet
otheobj
ect
.
Pr
ivat
e
o Thepr
ivat
ekey
wor
dset
samet
hodorv
ari
abl
etoonl
ybe
accessi
blef
rom wi
thi
nthecl
ass.
o Thi
smeanst
hatwecannotaccessormodi
fyt
hepr
oper
tyf
rom
out
sideofthecl
assev
eni
fwehav
ear
efer
encet
oitasshowni
n
Fi
gure2.6.
Pr
otect
ed
o Pr
otect
eddat
amember
sandmet
hodsar
eonl
yaccessi
blebyt
he
cl
assesofthesamepackageandt
hesubcl
assespr
esenti
nany
package.
I
nUML,
the"
+"sy
mbol
isusedt
odenot
epubl
i
cpr
oper
ti
esandmet
hods,
t
he"
–"sy
mbol
forpr
ivat
eones,
andt
he"
#"sy
mbol
forpr
otect
edones.
St
ati
cMembers
A st
ati
cmemberi
sapr
oper
tyormet
hodt
hatal
linst
ancesofacl
ass
shar
eandt
her
eisonl
yonev
aluef
oracl
ass’
sst
ati
cpr
oper
ty.
Toi
l
lust
rat
ehowast
ati
cmemberi
sshar
edbet
weeni
nst
ancesofacl
ass,
wewi
l
laddt
hest
ati
cpr
oper
tyar
ti
stCountt
oourAr
ti
stcl
ass,
andusei
tto
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 30
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
keepacountofhowmanyAr
ti
stobj
ect
sar
ecur
rent
lyi
nst
ant
iat
ed.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 31
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Thi
svar
iabl
eisdecl
aredst
ati
cbyi
ncl
udi
ngt
hest
ati
ckey
wor
dint
he
decl
arat
ionasshowni
nli
sti
ng2.
5
Li
sti
ng2.
5:Cl
assdef
ini
ti
onmodi
fi
edwi
thst
ati
cmember
s
Not
icet
hatwedonotr
efer
enceast
ati
cpr
oper
tyusi
ngt
he$t
his-
>sy
ntax
,
butr
atheri
thasi
tsownsel
f:
:sy
ntax
.
Ther
ati
onal
ebehi
ndt
hischangei
stof
orcet
hepr
ogr
ammert
o
under
standt
hatt
hev
ari
abl
eisst
ati
candnotassoci
atedwi
thani
nst
ance
(
$thi
s).
Thi
sst
ati
cvar
iabl
ecanal
sobeaccessedwi
thoutanyi
nst
anceofanAr
ti
st
obj
ectbyusi
ngt
hecl
assname,
thati
s,v
iaAr
ti
st:
:
$ar
ti
stCount
.
St
ati
cmet
hodsar
esi
mil
art
ost
ati
cpr
oper
ti
esi
nthatt
heyar
egl
obal
l
y
accessi
ble(
ifpubl
i
c)andar
enotassoci
atedwi
thpar
ti
cul
arobj
ect
s.
I
tshoul
dbenot
edt
hatst
ati
cmet
hodscannotaccessi
nst
ancemember
s.
St
ati
cmet
hodsar
ecal
l
edusi
ngt
hesamedoubl
ecol
onsy
ntaxasst
ati
c
pr
oper
ti
es.
Cl
assConst
ant
s
Toaddaproper
tyt
oacl
asst
hati
sconst
antassameasst
ati
cmember
s
butwi
thaconstkey
wor
d.
However
,const
antv
aluescanbest
oredmor
eef
fi
cient
lyascl
ass
const
ant
ssol
ongast
heyar
enotcal
cul
atedorupdat
ed.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 32
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Unl
i
keal
lot
herv
ari
abl
es,
const
ant
sdon’
tuset
he$sy
mbol
whendecl
ari
ng
orusi
ngt
hem.
They can be accessed bot
hinsi
de and out
side t
he cl
ass usi
ng
sel
f:
:EARLI
EST_
DATE i
nthecl
assandcl
assRef
erence:
:
EARLI
EST_
DATE
out
side.
Obj
ect
-Or
ient
edDesi
gn
Wi
tht
hebasi
cunder
standi
ngofhowt
odef
ineandusecl
assesandobj
ect
s,we
canst
artt
ogett
hebenef
it
sofsof
twar
eengi
neer
ingpat
ter
ns,whi
chencour
age
under
standabl
eandl
esser
ror
-pr
onecode.
The obj
ect
-or
ient
ed desi
gn ofsof
twar
e of
fer
s many benef
it
sint
erms of
modul
ari
ty,
test
abi
l
ity
,andr
eusabi
l
ity
.
Dat
aEncapsul
ati
on
Themosti
mpor
tantadv
ant
aget
oobj
ect
-or
ient
eddesi
gni
sthepossi
bil
i
tyof
encapsul
ati
on,whi
ch gener
all
yref
erst
orest
ri
cti
ng access t
o an obj
ect
’s
i
nter
nal
component
s.
Anot
herwayofunder
standi
ngencapsul
ati
oni
s:i
tist
hehi
dingofanobj
ect
’s
i
mpl
ement
ati
ondet
ail
s.
Apr
oper
lyencapsul
atedcl
asswi
l
ldef
ineani
nter
facet
othewor
ldi
nthef
orm
ofi
tspubl
i
cmet
hods,andl
eav
eit
sdat
a,t
hati
s,i
tspr
oper
ti
es,hi
dden(
thati
s,
pr
ivat
e).
I
fapr
oper
lyencapsul
atedcl
assmakesi
tspr
oper
ti
espr
ivat
e,t
hent
het
ypi
cal
appr
oachi
stowr
it
emet
hodsf
oraccessi
ngandmodi
fyi
ngpr
oper
ti
esr
ather
t
hanal
l
owi
ngt
hem t
obeaccesseddi
rect
ly.
These methods ar
e commonl
y cal
l
ed get
ter
s and set
ter
s(oraccessor
s and
mutat
ors)
.
Aget
tert
oret
urnav
ari
abl
e’
sval
uei
sof
tenv
eryst
rai
ght
for
war
dandshoul
d
notmodi
fyt
hepr
oper
ty.
I
tisnor
mal
l
ycal
l
edwi
thoutpar
amet
ers,
andr
etur
nst
hepr
oper
tyf
rom wi
thi
n
t
hecl
ass.
Set
termet
hodsmodi
fypr
oper
ti
es,
andal
l
owext
ral
ogi
ctobeaddedt
o
pr
eventpr
oper
ti
esf
rom bei
ngsett
ost
rangev
alues.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 33
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Ex:
get
terf
unct
ion:
get
() set
terf
unct
ion:
set
()
Thekeyadv
ant
ageofusi
ngget
ter
sandset
ter
sar
e:
o Thecl
asscanhandl
ether
esponsi
bil
i
tyofensur
ingi
tsowndat
aval
i
dat
ion.
o Andsi
ncet
heset
terf
unct
ionsar
eper
for
mingv
ali
dat
ion,
the
const
ruct
orf
ort
hecl
assshoul
duset
heset
terf
unct
ionst
osett
he
v
alues.
I
nher
it
ance
Al
ongwi
thencapsul
ati
on,
inher
it
ancei
soneoft
het
hreekeyconcept
sin
obj
ector
ient
eddesi
gnandpr
ogr
ammi
ng.
I
nher
it
anceenabl
esust
ocr
eat
enewPHPcl
assest
hatr
euse,
ext
end,
and
modi
fyt
hebehav
iort
hati
sdef
inedi
nanot
herPHPcl
ass.
PHPonl
yal
l
owsy
out
oinher
itf
rom onecl
assatat
ime.
Aclasst
hati
sinher
it
ingf
rom anot
hercl
assi
ssai
dtobeasubcl
assorader
ived
cl
ass.
Thecl
asst
hati
sbei
ngi
nher
it
edf
rom i
sty
pical
l
ycal
l
edasuper
classorabasecl
ass.
Whenacl
assi
nher
it
sfr
om anot
hercl
ass,i
tinher
it
sal
lofi
tspubl
i
cand
pr
otect
edmet
hodsandpr
oper
ti
es.Fi
gur
e2.
7il
l
ust
rat
eshow i
nher
it
ancei
s
showni
naUMLcl
assdi
agr
am.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 34
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Fi
gur
e2.
7:UMLcl
assdi
agr
am wi
thi
nher
it
ance
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 35
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Justasi
nJav
a,aPHPcl
assi
sdef
inedasasubcl
assbyusi
ngt
heext
endskey
wor
d.
Ref
erenci
ngBaseCl
assMember
s
o Asubcl
assi
nher
it
sthepubl
i
candpr
otect
edmember
soft
hebasecl
ass.
Thusi
nthef
oll
owi
ngcodebasedonFi
gur
e2.
7,bot
hoft
her
efer
ences
wi
l
lwor
kbecausei
tisasi
fthebasecl
asspubl
i
cmember
sar
edef
ined
wi
thi
nthesubcl
ass.
I
nher
it
ingMet
hods
o Ev
erymet
hoddef
inedi
nthebase/
par
entcl
asscanbeov
err
iddenwhen
ext
endi
ngacl
ass,
bydecl
ari
ngaf
unct
ionwi
tht
hesamename.
o Toaccessapubl
i
corpr
otect
edmet
hodorpr
oper
tydef
inedwi
thi
nabase
cl
assf
rom wi
thi
nasubcl
ass,wedosobypr
efi
xi
ngt
hemembername
wi
thpar
ent
::
.Sot
o access t
he par
ent
’s t
oSt
ri
ng(
) met
hod we
woul
d si
mpl
y usepar
ent
::t
oSt
ri
ng(
).
Par
entConst
ruct
ors
o I
fwewantt
oinv
okeapar
entconst
ruct
ori
ntheder
ivedcl
ass’
s
const
ructor
,wecanuset
hepar
ent
::sy
ntaxandcal
ltheconst
ruct
oron
thefi
rstl
i
neparent
::
const
ruct
().
o Thi
sissi
mil
art
ocal
l
ingot
herpar
entmet
hods,
exceptt
hatt
ousei
twe
mustcal
li
tatt
hebegi
nni
ngofourconst
ruct
or.
Pol
ymor
phi
sm
Pol
ymor
phi
sm i
sthet
hir
dkeyobj
ect
-or
ient
edconcept(
alongwi
th
encapsul
ati
onandi
nher
it
ance)
.
Pol
ymor
phi
sm i
sthenot
iont
hatanobj
ectcani
nfactbemul
ti
plet
hingsatt
he
samet
ime.
Letusbegi
nwi
thani
nst
anceofaPai
nti
ngobj
ectnamed$guer
nicacr
eat
edas
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 36
WebTechnologyandI
t’
s 18CS6
Appl
i
cati
ons 3
fol
lows:
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 37
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Thev
ari
abl
e$guer
nicai
sbot
haPai
nti
ngobj
ectandanAr
tobj
ectduet
oit
s
i
nher
it
ance.
Theadv
ant
ageofpol
ymor
phi
sm i
sthatwecanmanageal
i
stofAr
tobj
ect
s,
andcal
lthesameov
err
iddenmet
hodoneach.
Obj
ectI
nter
faces
Anobj
ectinter
facei
sawayofdef
ini
ngaf
ormall
i
stofmet
hodst
hatacl
ass
musti
mpl
ementwi
thoutspeci
fyi
ngt
hei
rimpl
ement
ati
on.
I
nter
facespr
ovi
deamechani
sm f
ordef
ini
ngwhatacl
asscandowi
thout
speci
fyi
nghowi
tdoesi
t,whi
chi
sof
tenav
eryusef
uldesi
gnt
echni
que.
I
nter
faces ar
e def
ined usi
ng t
he i
nter
face key
wor
d,and l
ook si
mil
art
o
st
andar
dPHP cl
asses,exceptani
nter
facecont
ainsnopr
oper
ti
esandi
ts
met
hodsdonothav
emet
hodbodi
esdef
ined.
Ex:
Ani
nter
facecont
ainsonl
ypubl
i
cmet
hods,andi
nst
eadofhav
ingamet
hod
body
,eachmet
hodi
ster
minat
edwi
thasemi
col
on.
InPHP,acl
asscanbesai
dtoi
mpl
ementani
nter
face,usi
ng t
hei
mpl
ement
s
key
word.
Thi
smeanst
hent
hatt
hecl
assPai
nti
ngmustpr
ovi
dei
mpl
ement
ati
ons(
i.
e.,
nor
mal
met
hodbodi
es)f
ort
heget
Size(
)andget
PNG(
)met
hods.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 38
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Chapt
er3:
Err
orHandl
i
ngandVal
i
dat
ion
1. Whatar
eEr
ror
sandExcept
ions?
2. PHPEr
rorRepor
ti
ng
3. PHPEr
rorandExcept
ionHandl
i
ng
WhatAr
eEr
ror
sandExcept
ions?
Ev
ent
hebest
-wr
it
tenwebappl
i
cat
ioncansuf
ferf
rom r
unt
imeer
ror
s.
Mostcompl
exwebappl
i
cat
ionsmusti
nter
actwi
thext
ernalsy
stemssuchas
dat
abases,webser
vices,RSSf
eeds,emai
lser
ver
s,f
il
esy
stem,andot
her
ext
ernal
i
tiest
hatar
ebey
ondt
hedev
eloper
’scont
rol
.
Af
ail
urei
nanyoneoft
hesesy
stemswi
l
lmeant
hatt
hewebappl
i
cat
ionwi
l
l
no l
ongerr
un successf
ull
y.I
tisv
ital
l
yimpor
tantt
hatweb appl
i
cat
ions
gr
acef
ull
yhandl
esuchpr
obl
ems.
Ty
pesofEr
ror
s
Notev
erypr
obl
em i
sunexpect
edorcat
ast
rophi
c.Onemi
ghtsayt
hatt
her
ear
e
t
hreedi
ff
erentt
ypesofwebsi
tepr
obl
ems:
■Expect
eder
ror
s
■War
nings
■Fat
aler
ror
s
An expect
ed er
rori
s an er
rort
hatr
out
inel
yoccur
s dur
ing an appl
i
cat
ion.
Per
hapst
hemostcommonexampl
eoft
hist
ypewoul
dbeaner
rorasar
esul
tof
useri
nput
s,f
ori
nst
ance,
ent
eri
ngl
ett
erswhennumber
swer
eexpect
ed.
Anot
hert
ypeofer
rori
swar
nings,whi
char
epr
obl
emst
hatgener
ateaPHP
war
ningmessage(
whi
chmayormaynotbedi
spl
ayed)butwi
l
lnothal
tthe
execut
ion oft
he page.Fori
nst
ance,cal
l
ing a f
unct
ion wi
thouta r
equi
red
par
amet
erwi
l
lgener
ateawar
ningmessagebutnotst
opexecut
ion.
Thef
inalt
ypeofer
rori
sfat
aler
ror
s,whi
char
eser
iousi
nthatt
heexecut
ionof
t
hepagewi
l
lter
minat
eunl
esshandl
edi
nsomeway
.Theseshoul
dtr
ulybe
except
ionalandunexpect
ed,suchasar
equi
redi
nputf
il
ebei
ngmi
ssi
ngora
dat
abaset
abl
eorf
iel
ddi
sappear
ing.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 39
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Except
ions
Devel
oper
ssomet
imest
reatt
hewor
ds“
err
or”and“
except
ion”assy
nony
ms.
I
nthecont
extofPHP,
theydohav
edi
ff
erentmeani
ngs.
Aner
rori
ssomet
ypeofpr
obl
em t
hatgener
atesanonf
atalwar
ningmessage
ort
hatgener
atesaner
rormessaget
hatt
ermi
nat
est
hepr
ogr
am’
sexecut
ion.
Anexcept
ionr
efer
stoobj
ect
sthatar
eoft
ypeExcept
ionandwhi
char
eusedi
n
conj
unct
ionwi
tht
heobj
ect
-or
ient
edt
ry...cat
chl
anguageconst
ructf
or
deal
i
ngwi
thr
unt
imeer
ror
s.
PHPEr
rorRepor
ti
ng
PHPhasaf
lexi
bleandcust
omi
zabl
esy
stem f
orr
epor
ti
ngwar
ningsander
ror
s
t
hatcanbesetpr
ogr
ammat
ical
l
yatr
unt
imeordecl
arat
ivel
yatdesi
gn-
ti
mewi
thi
n
t
hephp.
ini
.
Ther
ear
ethr
eemai
ner
rorr
epor
ti
ngf
lags:
o er
ror
_repor
ti
ng
o di
spl
ay_
err
ors
o l
og_
err
ors
Themeani
ngofeachoft
hesei
simpor
tantandshoul
dbel
ear
nedbyPHPdev
eloper
s.
Theer
ror
_repor
ti
ngSet
ti
ng
o Theer
ror
_repor
ti
ngset
ti
ngspeci
fi
eswhi
cht
ypeofer
ror
sar
etober
epor
ted.
o I
tcanbesetpr
ogr
ammat
ical
l
yinsi
deanyPHPf
il
eby
usi
ngt
heer
ror
_repor
ti
ng(
)funct
ion:
o Thepossi
blel
evel
sforer
ror
_repor
ti
ngar
edef
inedbypr
edef
inedconst
ant
s
l
i
stssomeoft
hemostcommonv
alues.I
tiswor
thnot
ingt
hati
nsome
PHPenv
ironment
s,t
hedef
aul
tset
ti
ngi
szer
o,t
hati
s,nor
epor
ti
ng.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 40
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Thedi
spl
ay_
err
orsSet
ti
ng
o Thedi
spl
ay_
err
orset
ti
ng speci
fi
eswhet
herer
rormessagesshoul
d or
shoul
dnotbedi
spl
ayedi
nthebr
owser
.2I
tcanbesetpr
ogr
ammat
ical
l
yvi
a
t
hei
ni_
set
()f
unct
ion:
Thel
og_
err
orset
ti
ng
o Thel
og_
err
orset
ti
ngspeci
fi
eswhet
herer
rormessagesshoul
dorshoul
dnotbe
sentt
otheser
verer
rorl
og.I
tcanbesetpr
ogr
ammat
ical
l
yvi
athei
ni_
set
()
f
unct
ion:
PHPEr
rorandExcept
ionHandl
i
ng
Whenaf
atal
PHPer
roroccur
s,pr
ogr
am execut
ionwi
l
lev
ent
ual
l
yter
minat
e
unl
essi
tishandl
ed.
ThePHPdocument
ati
onpr
ovi
dest
womechani
smsf
orhandl
i
ngr
unt
ime
er
ror
s:pr
ocedur
aler
rorhandl
i
ngandt
hemor
eobj
ect
-or
ient
edexcept
ion
handl
i
ng.
Pr
ocedur
alEr
rorHandl
i
ng
I
nthepr
ocedur
alappr
oacht
oer
rorhandl
i
ng,
thepr
ogr
ammerneedst
oexpl
i
cit
ly
t
estf
orer
rorcondi
ti
onsaf
terper
for
mingat
askt
hatmi
ghtgener
ateaner
ror
.
I
nsuchacaseweneededt
otestf
oranddealwi
ther
ror
saf
tereachoper
ati
on
t
hatmi
ghtgener
ateaner
rorst
ate.
Whi
l
ethi
sappr
oach mi
ghtseem mor
est
rai
ght
for
war
d,i
tdoesr
equi
ret
he
pr
ogr
ammert
oknow aheadoft
imewhatcodei
sgoi
ngt
ogener
ateaner
ror
condi
ti
on.
Theadv
ant
ageoft
het
ry...cat
chmechani
sm i
sthati
tal
l
owst
hedev
elopert
o
handl
eawi
derv
ari
etyofexcept
ionsi
nasi
ngl
ecat
chbl
ock.
Evenwi
thexpl
i
citt
est
ingf
orer
rorcondi
ti
ons,t
her
ewi
l
lst
il
lbesi
tuat
ionswhen
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 41
WebTechnol
ogyandIt
’s 18CS6
Appl
i
cati
ons 3
anunfor
eseenerr
oroccur
s.I
nsuchacase,unl
essacust
om er
rorhandlerhas
beendef
ined,
PHPwi
l
lter
minat
etheexecut
ionoft
heappl
i
cat
ion.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 42
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
Li
sti
ng3.
1:Pr
ocedur
alappr
oacht
oer
rorhandl
i
ng
Obj
ect
-Or
ient
edExcept
ionHandl
i
ng
Whenar
unt
imeer
roroccur
s,PHPt
hrowsanexcept
ion.
Thi
sexcept
ioncanbecaughtandhandl
edei
therbyt
hef
unct
ion,cl
ass,orpage
t
hatgener
atedt
heexcept
ionorbyt
hecodet
hatcal
l
edt
hef
unct
ionorcl
ass.
I
fanexcept
ioni
snotcaught
,thenev
ent
ual
l
ythePHPenv
ironmentwi
l
lhandl
eit
byt
ermi
nat
ingexecut
ionwi
than“
UncaughtExcept
ion”
.
Li
keot
herobj
ect
-or
ient
edpr
ogr
ammi
ngl
anguages,PHPusest
het
ry.
..cat
ch
pr
ogr
ammi
ngconst
ructt
opr
ogr
ammat
ical
l
ydeal
wit
hexcept
ionsatr
unt
ime.
TheExcept
ioncl
asspr
ovi
desmet
hodsf
oraccessi
ngnotonl
ytheexcept
ion
message,butal
sot
hel
i
nenumberoft
hecodet
hatgener
atedt
heexcept
ionand
t
hest
ackt
race,bot
hofwhi
chcanbehel
pfulf
orunder
standi
ngwher
eandwhen
t
heexcept
ionoccur
red.
Li
sti
ng3.
2:Exampl
eoft
ry...cat
chbl
ock
Thef
inal
l
ybl
ocki
sopt
ional
.Anycodewi
thi
nitwi
l
lal
way
sbeexecut
edaf
tert
he
codei
nthet
ryori
nthecat
chbl
ocks,
eveni
fthatcodecont
ainsar
etur
n
st
atement
.
Cust
om Er
rorandExcept
ionHandl
ers
Whenawebappl
i
cat
ioni
sindev
elopment
,onecangener
all
ybecont
entwi
th
di
spl
ayi
ngand/
orl
oggi
nger
rormessagesandt
hent
ermi
nat
ingt
hescr
ipt
.
Butf
orpr
oduct
ionappl
i
cat
ions,
wewi
l
lli
kel
ywantt
ohandl
esi
gni
fi
canter
ror
sin
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 43
WebTechnologyandI
t’
s 18CS6
Appl
i
cati
ons 3
abet
terway.
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 44
WebTechnol
ogyandI
t’
s 18CS6
Appl
i
cati
ons 3
I
tispossi
blet
odef
ineourownhandl
erf
oruncaughter
ror
sandexcept
ions;t
he
mechani
sm f
ordoi
ng so v
ari
esdependi
ng uponwhet
hery
ouar
eusi
ng t
he
pr
ocedur
alorobj
ect
-or
ient
edmechani
sm f
orr
espondi
ngt
oer
ror
s.
I
fusi
ngt
hepr
ocedur
alappr
oach(
i.
e.notusi
, ngt
ry...cat
ch)
,wecandef
ineacust
om
er
ror
-handl
i
ngf
unct
ionandt
henr
egi
steri
twi
tht
heset
_er
ror
_handl
er(
)funct
ion.
I
fwear
eusi
ngt
heobj
ect
-or
ient
edexcept
ionappr
oachwi
tht
ry...cat
chbl
ocks,
wecandef
i om except
neacust ion-
handl
i
ngf
unct
ionandt
henr
egi
steri
twi
tht
he
set
_except
ion_
handl
er(
)funct
ion.
Li
sti
ng3.
3:Cust
om except
ionhandl
er
Prof.
Swat
hiJA,
Dept
.ofCSE,
CBI
T- 2022-
2023 Page
Kolar 45