Open Source Technology Polytechnic 6th Sem
Open Source Technology Polytechnic 6th Sem
sPHP?
PHPisaserv
erscr
ipt
ingl
anguage,
andapower
fult
oolf
ormaki
ngdy
nami
c
andi
nter
act
iveWebpages.
PHPi
sanacr
ony
mfor"
PHP:
Hyper
textPr
epr
ocessor
"
PHPi
sawi
del
y-
used,
opensour
cescr
ipt
ingl
anguage
PHPscr
ipt
sar
eexecut
edont
heser
ver
PHPi
sfr
eet
odownl
oadanduse
PHPisawidel
y-
used,f
ree,andef
fi
ciental
ter
nat
ivet
ocompet
it
orssuchas
Micr
osof
t'
sASP.
Whati
saPHPFi
l
e?
PHPf
il
escancont
aint
ext
,HTML,
CSS,
Jav
aScr
ipt
,andPHPcode
PHPcodeisexecut
edont
heser
ver
,andt
her
esul
tisr
etur
nedt
othe
br
owserasplai
nHTML
PHPf
il
eshav
eext
ensi
on"
.php"
Exampl
e
<!
DOCTYPEht
ml>
<?
php
echo"
Myf
ir
stPHPscr
ipt
!"
;
?
>
</
body
>
</
html
WhatCanPHPDo?
PHPcangener
atedy
nami
cpagecont
ent
PHPcancr
eat
e,open,
read,
wri
te,
del
ete,
andcl
osef
il
esont
heser
ver
PHPcancol
l
ectf
orm dat
a
PHPcansendandr
ecei
vecooki
es
PHPcanadd,
del
ete,
modi
fydat
ainy
ourdat
abase
PHPcanbeusedt
ocont
rol
user
-access
PHPcanencr
yptdat
a.
WhyPHP?
PHPr
unsonv
ari
ouspl
atf
orms(
Windows,
Linux,
Uni
x,MacOSX,
etc.
)
PHPi
scompat
ibl
ewi
thal
mostal
lser
ver
susedt
oday(
Apache,
II
S,et
c.)
PHPsuppor
tsawi
der
angeofdat
abases
PHPi
sfr
ee.Downl
oadi
tfr
om t
heof
fi
cial
PHPr
esour
ce:
www.
php.
net
PHPi
seasyt
olear
nandr
unsef
fi
cient
lyont
heser
versi
de.
SetUpPHPonYourOwnPC
Howev
er,
ify
ourser
verdoesnotsuppor
tPHP,
youmust
:
i
nst
all
awebser
ver
i
nst
all
PHP
i
nst
all
adat
abase,
suchasMy
SQL
Theoffi
cialPHPwebsi
te(PHP.net)hasi
nst
all
ati
oni
nst
ruct
ionsf
orPHP:
htt
p:/
/php.net
/manual
/en/
inst
all
.php
Basi
cPHPSy
ntax
APHPscr
iptst
art
swi
th<?
phpandendswi
th?
>
Thedef
aul
tfi
l
eext
ensi
onf
orPHPf
il
esi
s".
php"
.
APHPf
il
enor
mal
l
ycont
ainsHTMLt
ags,
andsomePHPscr
ipt
ingcode.
usesabui
l
t-i
nPHPf
unct
ion"
echo"t
oout
putt
het
ext"
Hel
l
oWor
ld!
"ona
webpage:
!
DOCTYPEht
ml>
<ht
ml>
<body
>
<h1>Myf
ir
stPHPpage</
h1>
<?
php
echo"
Hel
l
oWor
ld!
";
?
>
</
body
>
</
html
>
Out
put
.
Myf
ir
stPHPpage
Hel
l
oWor
ld
PHPCaseSensi
ti
vi
ty
I
nPHP, NOkeywords(e.
g.i
f,el
se,whi
le,echo,
etc.
),cl
asses,
funct
ions,
and
user
-def
inedf
unct
ionsarecase-
sensi
tiv
e.
Howev
er;
all
var
iabl
enamesar
ecase-
sensi
ti
ve.
Exampl
e
<!
DOCTYPEht
ml>
<ht
ml>
<body
>
<?
php
$col
or="
red"
;
echo"
Mycari
s".$col
or."
<br
>";
ECHO"
Mybi
kei
s".$col
or."
<br
>"
echo"
Myhousei
s".$COLOR."
<br
>";
echo"
Myboati
s".$coLOR."
<br
>";
?
>
</
body
>
</
html
>
Out
put
Mycari
sred
Mybi
kei
sred
Comment
sinPHP
Sy
ntaxf
orsi
ngl
eli
necomment
:
/
/Si
ngl
eli
necomment
#Si
ngl
eli
necomment
Sy
ntaxf
ormul
ti
plel
i
necomment
s
/
*Mul
ti
plel
i
nes
-
--
--
--
--
--
-
-
--
--
--
--
--
-*/
PHPVar
iabl
es
I
nPHP,avar
iabl
est
art
swi
tht
he$si
gn,
fol
l
owedbyt
henameoft
he
v
ari
abl
e.
I
tiscr
eat
edt
hemomenty
ouf
ir
stassi
gnav
aluet
oit
.
Av
ari
abl
enamemustst
artwi
thal
ett
erort
heunder
scor
echar
act
er
Av
ari
abl
enamecannotst
artwi
thanumber
Av ar
iabl
enamecanonlycont
ainal
pha-
numer
icchar
act
ersand
underscor
es(
A-z,
0-9,
and_)
Vari
abl
enamesar
ecase-
sensi
ti
ve(
$ageand$AGEar
etwodi
ff
erent
var
iabl
es)
<?
php
$t
xt="
Hel
l
owor
ld!
";
$x=5;
$y=10.
5;
?
>
echo$t
xt;
echo"
<br
>";
echo$x;
echo"
<br
>";
echo$y
;
?
>
</
body
>
</
html
>
Out
put
Hel
l
owor
ld!
5
10.
5
Gl
obal
andLocal
Scope
Avari
abledecl
aredout
sideafunct
ionhasaGLOBALSCOPEandcanonl
y
beaccessedoutsi
deafunct
ion.
Av ar
iabl
edecl
aredwithi
naf unct
ionhasaLOCALSCOPEandcanonl
ybe
accessedwit
hinthatf
uncti
on.
<!
DOCTYPEht
ml>
<ht
ml>
<body
>
<?
php
$y
=8;
f
unct
ionmy
Test
(){
$x=5;
echo"
<p>Var
iabl
exi
nsi
def
unct
ioni
s:$x
</p>"
;
}
my
Test
();
echo"
<p>Var
iabl
exout
sidef
unct
ioni
s:$x
</p>"
;
echo"
<p>Var
iabl
eyout
sidef
unct
ioni
s:$y
</p>"
;
?
>
</
body
>
</
html
out
put
Var
iabl
exi
nsi
def
unct
ioni
s:5
Var
iabl
exout
sidef
unct
ioni
s:
Var
iabl
eyout
sidef
unct
ioni
s:8
Theglobalkey
wor
disusedt
oaccessagl
obal
var
iabl
efr
om wi
thi
na
funct
ion.
<?
php
$x=5;
$y=10;
f
unct
ionmy
Test
(){
gl
obal
$x,
$y;
$y=$x+$y
;
}
my
Test
();
echo$y
;
?
>
out
put
s-15
PHPal sostoresall
global
var
iabl
esinanarraycal
led$GLOBALS[ i
ndex]
.
Theindexhol dst
henameoft hevar
iabl
e.Thisar
rayisalsoaccessi
blef
rom
wit
hinf unct
ionsandcanbeusedtoupdateglobalvar
iablesdi
rect
ly.
<?
php
$x=5;
$y=10;
f
unct
ionmy
Test
(){
$GLOBALS[
'
y']=$GLOBALS[
'
x']+$GLOBALS[
'
y']
;
}
my
Test
();
echo$y
;//out
put
s15
?
>
PHPThest
ati
cKey
wor
d
Normall
y,whenafuncti
oni scompl
eted/
execut
ed,al
lofi
tsv
ari
abl
esare
del
eted.However
,somet i
meswewantal ocalv
ari
abl
eNOTt obedel
eted.
Weneedi tf
orafurt
herjob.
Todot
his,
uset
hest
ati
ckey
wor
dwheny
ouf
ir
stdecl
aret
hev
ari
abl
e:
Exampl
e
<?
php
f
unct
ionmy
Test
(){
st
ati
c$x=0;
echo$x;
$x++;
}
my
Test
();
my
Test
();
my
Test
();
?
>
PHPCondi
ti
onal
Stat
ement
s
Veryoft
enwheny ouwri
tecode,
youwanttoperf
orm di
ff
erentacti
onsf
or
dif
fer
entcondi
ti
ons.Youcanusecondi
ti
onal
statement
siny ourcodet
odo
thi
s.
I
nPHPwehav
ethef
oll
owi
ngcondi
ti
onal
stat
ement
s:
i
fst
atement-execut
essomecodei
fonecondi
ti
oni
str
ue
i
f..
.el
sestatement-execut
essomecodei
facondi
ti
oni
str
ueand
anothercodeift
hatcondit
ioni
sfal
se
i
f.
..
elsei
f.
..
elsest
atement-execut
esdi
ff
erentcodesf
ormor
ethan
t
wocondi t
ions
switchstat
ement-sel
ect
soneofmanybl
ocksofcodet
obe
executed
i
fSt
atement
Thei
fst
atementexecut
essomecodei
fonecondi
ti
oni
str
ue.
Sy
ntax
i
f(condi
ti
on){
codet
obeexecut
edi
fcondi
ti
oni
str
ue;
}
Exampl
e
Out
put"
Hav
eagoodday
!"i
fthecur
rentt
ime(
HOUR)i
slesst
han20:
<?
php
$t=dat
e("
H")
;
i
f($t<"
20"
){
echo"
Hav
eagoodday
!";
}
?
>
i
f.
..
elseSt
atement
Theif
..
.el
sest
atementexecut
essomecodei
facondi
ti
oni
str
ueand
anot
hercodei
fthatcondi
ti
onisf
alse.
Sy
ntax
i
f(condi
ti
on){
codet
obeexecut
edi
fcondi
ti
oni
str
ue;
}el
se{
codet
obeexecut
edi
fcondi
ti
oni
sfal
se;
}
Exampl
e
<?
php
$t=dat
e("
H")
;
i
f($t<"
20"
){
echo"
Hav
eagoodday
!";
}el
se{
echo"
Hav
eagoodni
ght
!"
;
}
?
>
i
f.
..
elsei
f.
..
elseSt
atement
Theif
..
.el
sei
f.
..
elsest
atementexecut
esdi
ff
erentcodesf
ormor
ethant
wo
condi
ti
ons.
Sy
ntax
i
f(condi
ti
on){
codet
obeexecut
edi
fthi
scondi
ti
oni
str
ue;
}el
sei
f(condi
ti
on){
codet
obeexecut
edi
ffi
rstcondi
ti
oni
sfal
seandt
hiscondi
ti
oni
str
ue;
}el
se{
codet
obeexecut
edi
fal
lcondi
ti
onsar
efal
se;
}
Exampl
e
Out
put"Hav
eagoodmor ni
ng!
"ift
hecurrentt
imeisl
essthan10,and"Hav
e
agoodday!
"ift
hecur
rentt
imeisl
essthan20.Other
wiseitwi
l
loutput
"
Haveagoodnight
!"
:
<?
php
$t=dat
e("
H")
;
i
f($t<"
10"
){
echo"
Hav
eagoodmor
ning!
";
}el
sei
f($t<"
20"
){
echo"
Hav
eagoodday
!";
}el
se{
echo"
Hav
eagoodni
ght
!"
;
}
?
>
swi
tchSt
atement
Theswitchst
atementi
susedt
oper
for
m di
ff
erentact
ionsbasedon
di
ff
erentcondi
ti
ons.
Sy
ntax
swi
tch(
n){
casel
abel
1:
codet
obeexecut
edi
fn=l
abel
1;
br
eak;
casel
abel
2:
codet
obeexecut
edi
fn=l
abel
2;
br
eak;
casel
abel
3:
codet
obeexecut
edi
fn=l
abel
3;
br
eak;
.
..
def
aul
t:
codet
obeexecut
edi
fni
sdi
ff
erentf
rom al
ll
abel
s;
}
Exampl
e
<?
php
$f
avcol
or="
red"
;
swi
tch(
$fav
col
or){
case"
red"
:
echo"
Yourf
avor
it
ecol
ori
sred!
";
br
eak;
case"
blue"
:
echo"
Yourf
avor
it
ecol
ori
sbl
ue!
";
br
eak;
case"
green"
:
echo"
Yourf
avor
it
ecol
ori
sgr
een!
";
br
eak;
def
aul
t:
echo"
Yourf
avor
it
ecol
ori
snei
therr
ed,
blue,
norgr
een!
";
}
?
>
PHPLoops
Loopsar
eusedt oexecut
et hesamebl
ockofcodeagai
nandagai
n,asl
ong
asacert
aincondit
ioni
strue.
I
nPHP,
wehav
ethef
oll
owi
ngl
oopt
ypes:
whil
e-loopst
hroughabl
ockofcodeasl
ongast
hespeci
fi
ed
condi
ti
onistr
ue
do.
..
whi
l
e-loopsthr
oughabl
ockofcodeonce,
andt
henr
epeat
sthe
l
oopaslongast
hespeci
fi
edcondi
ti
onist
rue
f
or-l
oopst
hroughabl
ockofcodeaspeci
fi
ednumberoft
imes
f
oreach-l
oopst
hroughabl
ockofcodef
oreachel
ementi
nanar
ray
whi
l
eLoop
Thewhil
eloopexecut
esabl
ockofcodeasl
ongast
hespeci
fi
edcondi
ti
on
i
strue.
Sy
ntax
whi
l
e(condi
ti
oni
str
ue){
codet
obeexecut
ed;
}
Exampl
e
Theexampl
ebel
owdi
spl
ayst
henumber
sfr
om 1t
o5:
<?
php
$x=1;
whi
l
e($x<=5){
echo"
Thenumberi
s:$x<br
>";
$x++;
}
?
>
Dowhi
l
eloop
Thedo.
..
whil
eloopwill
alwaysexecutethebl
ockofcodeonce,i
twill
then
checkt
hecondit
ion,
andrepeattheloopwhil
ethespeci
fi
edcondit
ionis
tr
ue.
Sy
ntax
do{
codet
obeexecut
ed;
}whi
l
e(condi
ti
oni
str
ue)
;
Exampl
e
<?
php
$x=1;
do{
echo"
Thenumberi
s:$x<br
>";
$x++;
}whi
l
e($x<=5)
;
?
>
f
orLoop
Theforl
oopisusedwheny
ouknowi
nadv
ancehowmanyt
imest
hescr
ipt
shoul
drun.
Sy
ntax
f
or(
ini
tcount
er;
testcount
er;
incr
ementcount
er){
codet
obeexecut
edf
oreachi
ter
ati
on;
}
Exampl
e
<?
php
f
or(
$x=0;
$x<=10;
$x++){
echo"
Thenumberi
s:$x<br
>";
}
?
>
f
oreachLoop
Thefor
eachl
oopworksonlyonar
ray
s,andi
susedt
oloopt
hrougheach
key
/val
uepai
rinanarr
ay.
Sy
ntax
f
oreach(
$ar
rayas$v
alue){
codet
obeexecut
ed;
}
Exampl
e
<?
php
$col
ors=ar
ray
("r
ed"
,"gr
een"
,"bl
ue"
,"y
ell
ow"
);
f
oreach(
$col
orsas$v
alue){
echo"
$val
ue<br
>";
}
?
>