You are on page 1of 35

Whati

sPHP
o PHPs
tandsforHypertextPreproces
sor
.
o PHPi
sani
nterpretedl
anguage,i
.e.
,therei
snoneedforcompi
lati
on.
o PHPi
sas
erver
-si
des
cri
pti
ngl
anguage.
o PHPi
sfas
terthanothers
cri
pti
ngl
anguages
,forexampl
e,ASPandJSP.

PHPExampl
e
Wemus
tsavethePHPfi
lewi
tha.
phpextens
ion.Let'
sseeas
impl
ePHPexampl
e.

Fi
le:hel
lo.
php
1. <!
DOCTYPE> 
 
2. <html
>  
3. <body> 
 
4. <?php 
 
5. echo 
"<h2>Hel
lo 
by 
PHP</
h2>"

 
6. ?> 
 
7. </
body> 
 
8. </
html
>  

Output:

Hel
lobyPHP

PHPOper
ator
s
PHPOperatori
sas
ymboli
.eus
edtoperformoperati
onsonoperands
.Forexampl
e:

1. $num=10+20;
//
+ i
s the 
operator 
and 
10,
20 
are 
operands
  

I
ntheaboveexampl
e,+i
sthebi
nary+operator,10and20areoperandsand$numi
svari
abl
e.

PHPOperatorscanbecategori
zedi
nfol
lowi
ngforms
:

o Ari
thmeti
cOperators
o Compari
sonOperators
o Bi
twi
seOperators
o Logi
calOperators
o Stri
ngOperators
o I
ncrementi
ng/
Decrementi
ngOper
ators
o ArrayOperators
o TypeOperators
o Executi
onOperators
o ErrorControlOperators
o As
signmentOperators

Wecanal
socategori
zeoperatorsonbehal
fofoperands
.Theycanbecategori
zedi
n3forms
:

o UnaryOperators
:worksons
ingl
eoperandss
uchas++,--etc.
o Bi
naryOperators
:wor
ksontwooperandss
uchasbi
nary+,-,*
,/etc.
o TernaryOperators
:worksonthreeoperandss
uchas"
?:

PHPOperatorsPrecedence
o Let'
sseetheprecedenceofPHPoperatorswi
thas
soci
ati
vity.

Operators Addi
tionalI
nformati
on As
soci
ati
vity

cl
onenew cl
oneandnew non-
as
soci
ati
ve

[ array(
) l
eft

*
* ari
thmeti
c ri
ght

++--~(
int)(
float)(
stri
ng)(
array) i
ncrement/
decrementandtypes ri
ght
(
obj
ect)(
bool
)@

i
nstanceof types non-
as
soci
ati
ve

! l
ogi
cal(
negati
on) ri
ght

*/% ari
thmeti
c l
eft

+-. ari
thmeti
cands
tri
ng l
eft
concatenati
on

<<>> bi
twi
se(
shi
ft) l
eft

<<=>>= compari
son non-
as
soci
ati
ve

==!
====!
==<> compari
son non-
as
soci
ati
ve

& bi
twi
seAND l
eft
^ bi
twi
seXOR l
eft

| bi
twi
seOR l
eft

&& l
ogi
calAND l
eft

|
| l
ogi
calOR l
eft

?: ternary l
eft

=+=-=*
=**
=/=.
=%=&=|
=^=<<=>>= as
signment ri
ght
=>

and l
ogi
cal l
eft

xor l
ogi
cal l
eft

or l
ogi
cal l
eft

, manyus
es(
comma) l
eft

PHPComments
PHPcommentscanbeusedtodes
cri
beanylineofcodes
othatotherdevel
opercanunders
tandthecode
eas
ily.I
tcanalsobeus
edtohideanycode.

PHPs upportssi
nglel
ineandmul
til
inecomments
.Thes
ecommentsares
imi
lartoC/
C++andPerls
tyl
e(Uni
x
shel
ls tyl
e)comments
.

PHPSi
ngl
eLi
neComments
Therearetwowaystous
esi
ngl
eli
necommentsi
nPHP.

o /
/(C++s
tyl
esi
ngl
eli
necomment)
o #(
Uni
xShel
lstyl
esi
ngl
eli
necomment)

1. <?php 
 
2. /

thi
s i
s C++ 
styl
e s
ingl
e l
ine 
comment 
 
3. # 
thi
s i
s Uni
x Shel

styl
e s
ingl
e l
ine 
comment 
 
4. echo 
"Wel
come 
to 
PHP 
singl
e l
ine 
comments
"; 
 
5. ?> 
 

Output:
Wel
cometoPHPs
ingl
eli
necomments

PHPDataTypes
PHPdatatypesareusedtoholddifferenttypesofdataorval
ues
.PHPs
upports8pri
miti
vedatatypes
thatcanbecategori
zedfurtheri
n3types :

1. Scal
arTypes
2. CompoundTypes
3. Speci
alTypes

PHPDataTypes
:Scal
arTypes
Thereare4s
cal
ardatatypesi
nPHP.

1. bool
ean
2. i
nteger
3. fl
oat
4. s
tri
ng

PHPDataTypes
:CompoundTypes
Thereare2compounddatatypesi
nPHP.

1. array
2. obj
ect

PHPDataTypes
:Speci
alTypes
Thereare2s
peci
aldatatypesi
nPHP.

1. res
ource
2. NULL

I
nteger
Thisdatatypeholdsonl
ynumericval
ues.I
tstoresonl
ywhol
enumberwi
thnofracti
onalcomponent.The
rangeofintegersmustl
iebetween-2^31to2^31.

Syntax
Integerscanbedefi
nedi
ndeci
mal
(bas
e10)
,hexadeci
mal
(bas
e16)
,octal
(bas
e8)orbi
nary(
bas
e2)
notation.

Exampl
e1

1.<?php  
2. 
  
 $x=123;
  
3. 
  
 echo 
$x;
  
4.?> 
 
Fl
oat
Thisdatatyperepres
entsdecimalvalues
.Thefl
oat(
floati
ngpoi
ntnumber)i
sanumberwi
thadeci
mal
poi
ntoranumberi nexponenti
alform.

Syntax

1.$a=1.234;
  
2.$x=1.2e4;
  
3.$y=7E-10; 
 

Exampl
e1

1.<?php  
2. 
  
 $x=22.41; 
 
3. 
  
 echo 
$x; 
 
4.?>  
CompoundTypes
Thereare2compounddatatypesi
nPHP.

1. Array
2. Obj
ect

Array:
Thearrayisacollectionofheterogeneous(dis
simi
lar)datatypes
.
Normalvari
ablecans toresi
ngl
eval ue,arraycanstoremulti
plevalues
.
Thearraycontainsanumberofel ements ,andeachelementisacombinati
onofel
ementkeyandel
ement
val
ue.

SYNTAXOFARRAYDECLARATI
ON:
1. Vari
abl
e_name 

array 
(el
ement1,
 el
ement2,
 el
ement3,
 el
ement4.
..
..
.)
  

PHPArrayTypes
Thereare3typesofarrayi
nPHP.

1. I
ndexedArray
2. As
soci
ati
veArray
3. Mul
tidi
mens
ionalArray

PHPI
ndexedArray
PHPindexisrepres
entedbynumberwhichstartsfrom0.Wecans torenumber,s
tri
ngandobj
ecti
nthe
PHParray.Al
lPHParrayelementsareass
ignedtoani ndexnumberbydefaul
t.

Therearetwowaystodefi
nei
ndexedarray:

1s
tway:

1. $s
eas
on=array(
"summer"
,"
winter"
,"
spri
ng"
,"
autumn"
);
  

2ndway:

1. $s
eas
on[
0]="
summer"

 
2. $s
eas
on[
1]="
winter"

 
3. $s
eas
on[
2]="
spri
ng"

 
4. $s
eas
on[
3]="
autumn"

 

Exampl
e
Fi
le:array1.
php
1. <?php 
 
2. $s
eas
on=array(
"summer"
,"
winter"
,"
spri
ng"
,"
autumn"
);
  
3. echo 
"Seas
on 
are:
 $s
eas
on[
0],
 $s
eas
on[
1],
 $s
eas
on[
2] 
and 
$seas
on[
3]"

 
4. ?> 
 

Output:

Seas
onare:s
ummer,wi
nter,s
pri
ngandautumn

Fi
le:array2.
php

1. <?php 
 
2. $s
eas
on[
0]="
summer"

 
3. $s
eas
on[
1]="
winter"

 
4. $s
eas
on[
2]="
spri
ng"

 
5. $s
eas
on[
3]="
autumn"

 
6. echo 
"Seas
on 
are:
 $s
eas
on[
0],
 $s
eas
on[
1],
 $s
eas
on[
2] 
and 
$seas
on[
3]"

 
7. ?> 
 

Output:

Seas
onare:s
ummer,wi
nter,s
pri
ngandautumn
Exampl
e1

<?php 
 
 
 
  
$arr= 
array(
10,
20,
30)

 
 
 
  
pri
nt_r(
$arr)

 
?> 
 

1.PHPAs
soci
ati
veArray
Wecanas
soci
atenamewi
theacharrayel
ementsi
nPHPus
ing=>s
ymbol
.

Therearetwowaystodefi
neas
soci
ati
vearray:

1s
tway:

1. $s
alary=array(
"Sai
"=>"
350000"
,"
raj
u"=>"
450000"
,"
Karti
k"=>"
200000"
);
  

2ndway:

1. $s
alary[
"Sai
"]="
350000"

 
2. $s
alary[
"raj
u"]
="450000"

 
3. $s
alary[
"Karti
k"]
="200000"

 

Exampl
e
Fi
le:arrayas
soci
ati
ve1.
php
1. <?php 
 
  
2. $s
alary=array(
"Sai
"=>"
350000"
,"
raj
u"=>"
450000"
,"
Karti
k"=>"
200000"
);
  
 
 
3. echo 
"Sai
 sal
ary:
 "
.$s
alary[
"Sonoo"
].
"<br/
>";
  
4. echo 
"raj
u s
alary:
 "
.$s
alary[
"John"
].
"<br/
>";
  
5. echo 
"Karti
k s
alary:
 "
.$s
alary[
"Karti
k"]
."
<br/
>";
  
6. ?> 
 
  

Output:

Saisalary:350000
rajusalary:450000
Kartiksalary:200000
Fil
e:arrayassoci
ati
ve2.
php
1. <?php 
 
  
2. $s
alary[
"Sonoo"
]="
350000"

  
 
3. $s
alary[
"John"
]="
450000"

  
 
4. $s
alary[
"Karti
k"]
="200000"

  
 
5. echo 
"Sonoo 
sal
ary:
 "
.$s
alary[
"Sonoo"
].
"<br/
>";
  
6. echo 
"John 
sal
ary:
 "
.$s
alary[
"John"
].
"<br/
>";
  
7. echo 
"Karti
k s
alary:
 "
.$s
alary[
"Karti
k"]
."
<br/
>";
  
8. ?> 
 
  

Output:
Sonoosalary:350000
Johnsalary:450000
Karti
ks alary:200000

PHPMul
tidi
mens
ionalArray
PHPmul ti
dimens
ionalarrayisalsoknownasarrayofarrays
.Ital
lowsyoutostoretabul
ardatainan
array.PHPmulti
dimensionalarraycanberepresentedi
ntheformofmatrixwhichisrepres
entedbyrow*
column.

Defi
niti
on
1. $emp 

array 
 
2.  
 

 
3.  
 
array(
1,"
sai
",
400000)

 
4.  
 
array(
2,"
raj
u",
500000)

 
5.  
 
array(
3,"
rahul
",
300000)
  
6.  
 
); 
 

PHPMul
tidi
mens
ionalArrayExampl
e
Let'
sseeasimpleexampl
eofPHPmulti
dimens
ionalarraytodi
spl
ayfol
lowi
ngtabul
ardata.I
nthi
sexampl
e,
wearedispl
aying3rowsand3columns.

I
d Name Sal
ary

1 s
ai 400000

2 raj
u 500000

3 rahul 300000

Fi
le:mul
tiarray.
php

1. <?php 
 
  
2. $emp 

array 
 
3.  
 

 
4.  
 
array(
1,"
sai
",
400000)

 
5.  
 
array(
2,"
raj
u",
500000)

 
6.  
 
array(
3,"
rahul
",
300000)
  
7.  
 
); 
 
8.  
 
9. for 
($row 

0; 
$row 

3; 
$row++)
 {
  
10. 
 
for 
($col
 = 
0; 
$col
 < 
3; 
$col
++)
 {
  
11. 
 
  
echo 
$emp[
$row]
[$col
].

 "

 
12. 
 
}  
13. 
 
echo 
"<br/
>";
  
14.}
  
15.?> 
 
  

Output:

1sai400000
2raju500000
3rahul300000

Speci
alTypes
Thereare2s
peci
aldatatypesi
nPHP

1. Res
ource
2. Nul
l

Res
ourceDatatype:
Itreferstheexternalresourcesl
ikedatabas
econnecti
on,FTPconnecti
on,filepointers,etc.I
nsi
mpl
e
terms,aresourceisaspecialvari
ablewhichcarryi
ngareferencetoanexternalresource.
Exampl
e1
1. <?php 
 
2.  
 
  
$conn 

ftp_connect(
"127.
0.0.
1")
 or 
die(
"Coul
d not 
connect"
);
  
3.  
 
  
echo 
get_res
ource_type(
$conn)

 
4. ?> 
 

PHPi
s_nul
l(
)functi
on
Byus i
ngtheis_nul
lfunction,wecancheckwhetherthevari
abl
eisNULLornot.Thi
sfuncti
onwas
introducedi
nPHP4. 0.4.
SYNATX:
1. bool
 i
s_nul


mixed 
$var 

 

Parameter

Parameter Des
cri
pti
on I
scompul
sory

var Thevari
abl
ebei
ngeval
uated. compul
sory

ReturnType:
ThePHPi
s_nul
l(
)functi
onreturnstruei
fvari
snul
l,otherwi
sefal
se.
I
mportantNote:
Wecanuns
etthevari
abl
eval
uebyus
ingtheuns
etfuncti
on.
Exampl
e1
<?php 
 
 
 
  
$var1 

TRUE;
  
  
 
  
if 
(i
s_nul
l(
$var1)

 
  
 
  
  
 
 { 
 
  
 
  
  
 
  
  
 
echo 
'
Vari
abl
e i
s  
NULL'
;
  
  
 
  
  
 
 } 
 
  
 
  
  
 
 el
se 
 
  
 
  
  
 
 { 
 
  
 
  
  
 
  
  
 
echo 
'
Vari
abl
e i
s not 
NUL
L';
  
  
 
  
  
 
 } 
 
 ?> 
 

Exampl
e2
1. <?php 
 
2.  
 
  
$x= 
100;
  
3.  
 
  
uns
et(
$x)

 
4.  
 
  
echo 
is_nul
l(
$x)

 
5. ?> 
 

Exampl
e3

1. <?php 
 
  
2.  
 
  
$x 

NULL;
  
 
3.  
 
  
$y 

"\0"

 
4.  
 
  
is_nul
l(
$x)
 ? 
pri
nt_r(
"True\n"


pri
nt_r(
"Fal
se\n"
);
  
5.  
 
  
echo 
"<br/
>";
  
6.  
 
  
is_nul
l(
$y)
 ? 
pri
nt_r(
"True\n"


pri
nt_r(
"Fal
se\n"
);
  
7. ?> 
 

PHPI
fEl
se
PHPi
fel
ses
tatementi
sus
edtotes
tcondi
tion.Therearevari
ouswaystous
eifs
tatementi
nPHP.
o i
f
o i
f-el
se
o i
f-el
se-i
f
o nes
tedi
f

PHPI
fStatement
PHPi fstatementi
sexecutedi
fcondi
tioni
strue.
Syntax
1. i
f(condi
tion)

 
2. /
/code 
to 
be 
executed 
 
3. }
  
Exampl
e

1. <?php 
 
2. $num=12;
  
3. i
f($num<100)

 
4. echo 
"$num 
is 
les
s than 
100"

 
5. }
  
6. ?> 
 

Output:

12i
sles
sthan100

PHPSwi
tch
PHPswitchs tatementi
sus
edtoexecuteones
tatementfrommul
tipl
econdi
tions
.Itworksl
ikePHPi
f-
el
se-i
fs tatement.
Syntax
1. s
witch(
expres
sion)

  
 
  
2. cas
e val
ue1:
  
 
  
 
3.  
//
code 
to 
be 
executed 
 
4.  
break;
  
5. cas
e val
ue2:
  
 
  
 
6.  
//
code 
to 
be 
executed 
 
7.  
break;
  
8. .
..
..

  
 
  
9. defaul
t: 
 
  
  
 
10. 
code 
to 
be 
executed 
if 
all
 cas
es 
are 
not 
matched;
  
 
 
11.}
  

PHPSwi
tchExampl
e

1. <?php 
 
  
2. $num=20;
  
 
 
3. s
witch(
$num)

  
 
4. cas
e 10:
  
 
 
5. echo(
"number 
is 
equal

to 
10"
);
  
 
 
6. break;
  
 
 
7. cas
e 20:
  
 
 
8. echo(
"number 
is 
equal
 to 
20"
);
  
 
 
9. break;
  
 
 
10.cas
e 30:
  
 
 
11.echo(
"number 
is 
equal
 to 
30"
);
  
 
 
12.break;
  
 
 
13.defaul
t: 
 
  
14.echo(
"number 
is 
not 
equal
 to 
10,
 20 
or 
30"
);
  
 
 
15.}
  
 
16.?> 
 
  

Output:

numberi
sequalto20
PHPForLoop
PHPforloopcanbeusedtotraversesetofcodeforthes peci
fiednumberofti
mes
.
Itshoul
dbeusedifnumberofiterati
oni sknownotherwi
seus ewhi
leloop.
Syntax
1. for(
ini
tial
izati
on;
 condi
tion;
 i
ncrement/
decrement)

 
2. /
/code 
to 
be 
executed 
 
3. }
  
Fl
owchart Exampl
e

1. <?php 
 
2. for(
$n=1;
$n<=10;
$n++)

 
3. echo 
"$n<br
/>"

 
4. }
  
5. ?> 
 

PHPWhi
leLoop
PHPwhi
lel
oopcanbeus
edtotravers
esetofcodel
ikeforl
oop.

I
tshoul
dbeus
edi
fnumberofi
terati
oni
snotknown.

Syntax

1. whi
le(
condi
tion)

 
2. /
/code 
to 
be 
executed 
 
3. }
  
Al
ternati
veSyntax

1. whi
le(
condi
tion)

 
2. /
/code 
to 
be 
executed 
 
3.  
 
4. endwhi
le;
  

PHPWhi
leLoopFl
owchart

PHPWhi
leLoopExampl
e

1. <?php 
 
2. $n=1;
  
3. whi
le(
$n<=10)

 
4. echo 
"$n<br
/>"

 
5. $n++;
  
6. }
  
7. ?> 
 

Al
ternati
veExampl
e

1. <?php 
 
2. $n=1;
  
3. whi
le(
$n<=10)

 
4. echo 
"$n<br
/>"

 
5. $n++;
  
6. endwhi
le;
  
7. ?> 
 

PHPdowhi
lel
oop
PHPdowhilel
oopcanbeusedtotravers
esetofcodel
ikephpwhi
lel
oop.ThePHPdo-whi
lel
oopi
s
guar
anteedtorunatleas
tonce.

I
texecutesthecodeatl
eas
toneti
meal
waysbecaus
econdi
tioni
scheckedafterexecuti
ngthecode.

Syntax

1. do{
  
2. /
/code 
to 
be 
executed 
 
3. }
whi
le(
condi
tion)

 

Fl
owchart

Exampl
e

1. <?php 
 
2. $n=1;
  
3. do{
  
4. echo 
"$n<br
/>"

 
5. $n++;
  
6. }
whi
le(
$n<=10)

 
7. ?> 
 

PHPBreak
PHPbreaks
tatementbreakstheexecuti
onofcurrentfor,while,do-whi
le,swi
tchandfor-eachl
oop.I
f
youus
ebreakinsi
deinnerloop,i
tbreakstheexecuti
onofi nnerlooponly.

Syntax

1. j
ump 
statement;
  
2. break;
  

Fl
owchart

PHPBreak:i
nsi
del
oop
Let'
sseeas
impl
eexampl
etobreaktheexecuti
onofforl
oopi
fval
ueofii
sequalto5.

1. <?php 
 
2. for(
$i=1;
$i<=10;
$i++)

 
3. echo 
"$i
 <br
/>"

 
4. i
f($i
==5)

 
5. break;
  
6. }
  
7. }
  
8. ?> 
 
Output:

1
2
3
4
5

PHPBreak:i
nsi
dei
nnerl
oop
ThePHPbreaks
tatementbreakstheexecuti
onofi
nnerl
ooponl
y.

1. <?php 
 
2. for(
$i=1;
$i<=3;
$i++)

 
3.  
for(
$j=1;
$j<=3;
$j++)

 
4.  
 
echo 
"$i
  
 $j
<br/
>";
  
5.  
 
if(
$i==2 
&& 
$j==2)

 
6.  
 
 break;
  
7.  
 
}  
8.  

 
9. }
  
10.?> 
 

PHPFormHandl
ing
Wecancreateandus
eformsi
nPHP.Togetformdata,weneedtous
ePHPs
upergl
obal
s$_
GETand$_POST.

Theformreques
tmaybegetorpos
t.Toretri
evedatafr
omgetreques
t,weneedtous
e$_GET,forpos
t
reques
t$_POST.

PHPGetForm
Getrequestisthedefaultformreques
t.Thedatapas
sedthroughgetreques
tisvis
ibleontheURL
brows
ers oitisnotsecured.Youcansendl
imi
tedamountofdatathroughgetrequest.

Let'
sseeas
impl
eexampl
etorecei
vedatafromgetreques
tinPHP.

Fi
le:form1.
html
1. <form 
acti
on="
wel
come.
php"
 method="
get"
>  
2. Name:
 <i
nput 
type="
text"
 name="
name"
/> 
 
3. <i
nput 
type="
submi
t" 
val
ue="
vis
it"
/> 
 
4. </
form> 
 
Fi
le:wel
come.
php
1. <?php 
 
2. $name=$_GET[
"name"
];
//
recei
ving 
name 
fiel
d val
ue 
in 
$name 
vari
abl
e  
3. echo 
"Wel
come,
 $name"

 
4. ?> 
 

PHPPos
tForm
Pos
trequesti
swidelyusedtosubmi
tformthathavel
argeamountofdatas
uchasfi
leupl
oad,i
mage
upl
oad,l
ogi
nform,regis
trati
onformetc.

Thedatapass
edthroughpostreques
ti snotvi
sibl
eontheURLbrows
ers
oiti
ssecured.Youcans
end
l
argeamountofdatathroughpostrequest.

Let'
sseeas
impl
eexampl
etorecei
vedatafrompos
treques
tinPHP.

Fi
le:form1.
html
1. <form 
acti
on="
logi
n.php"
 method="
pos
t"> 
 
 
2. <tabl
e> 
 
 
3. <tr><td>Name:
</td><td> 
<input 
type="
text"
 name="
name"
/></
td></
tr> 
 
4. <tr><td>Pas
sword:
</td><td> 
<input 
type="
pas
sword"
 name="
pas
sword"
/></
td></
tr> 
 
 
5. <tr><td 
col
span="
2"><i
nput 
type="
submi
t" 
val
ue="
logi
n"/
>  
</td></
tr> 
 
6. </
tabl
e> 
 
7. </
form> 
 
 
Fi
le:l
ogi
n.php
1. <?php 
 
2. $name=$_POST[
"name"
];
//
recei
ving 
name 
fiel
d val
ue 
in 
$name 
vari
abl
e  
3. $pas
sword=$_POST[
"pas
sword"
];
//
recei
ving 
pas
sword 
fiel
d val
ue 
in 
$pas
sword 
vari
abl
e  
4.  
 
5. echo 
"Wel
come:
 $name,
 your 
pas
sword 
is:
 $pas
sword"

 
6. ?> 
 

next→← pr
ev

PHPI
ncl
udeFi
le
PHPal
lowsyoutoi
ncl
udefi
les
othatapagecontentcanbereus
edmanyti
mes
.Therearetwowaysto
i
ncl
udefi
lei
nPHP.

1. i
ncl
ude
2. requi
re

Advantage

CodeReus
abi
lity:
 Bythehel
pofi
ncl
udeandrequi
recons
truct,wecanreus
eHTMLcodeorPHPs
cri
pt
i
nmanyPHPs
cri
pts
.

PHPi
ncl
udeexampl
e
PHPi
ncl
udei
sus
edtoi
ncl
udefi
leonthebas
isofgi
venpath.Youmayus
erel
ati
veorabs
olutepathof
thefi
le.Let'
sseeas
impl
ePHPi
ncl
udeexampl
e.

Fi
le:menu.
html
1. <a 
href="
http:
//
www.
javatpoi
nt.
com"
>Home</
a> 

 
 
2. <a 
href="
http:
//
www.
javatpoi
nt.
com/
php-tutori
al"
>PHP</
a> 

 
 
3. <a 
href="
http:
//
www.
javatpoi
nt.
com/
java-tutori
al"
>Java</
a> 

 
  
4. <a 
href="
http:
//
www.
javatpoi
nt.
com/
html
-tutori
al"
>HTML
</a> 
 

Fi
le:i
ncl
ude1.
php
1. <?php 
incl
ude(
"menu.
html
")

?> 
 
2. <h1>Thi
s i
s Mai
n Page</
h1> 
 

PHPCooki
e
PHPcooki
eisas
mal
lpi
eceofi
nformati
onwhi
chi
sstoredatcl
ientbrows
er.I
tisus
edtorecogni
zethe
user.

Cookieiscreatedatservers
ideands avedtoclientbrowser.Eachti
mewhencl i
ents endsreques
ttothe
server,cook
ieisembeddedwithrequest.Suchway,cooki
ecanberecei vedattheserverside.

I
nshort,cooki
ecanbecreated,s
entandrecei
vedats
erverend.

S
etti
ngCo
oki
eswi
thPHP
PHPprovi
ded 
setcooki
e()
 functi
ontos
etacooki
e.Thi
sfuncti
onrequi
resuptos
ixargumentsands
houl
d
becal
ledbefore<html
>tag.Foreachcooki
ethi
sfuncti
onhastobecal
leds
eparatel
y.

s
etcooki
e(name,val
ue,expi
re,path,domai
n,s
ecuri
ty)
;

Herei
sthedetai
lofal
lthearguments−
 Name 
−Thi
ssetsthenameofthecooki
eandi
sstor
edi
nanenvi
ronmentvar
iabl
ecal
led
HTTP_COOKI
E_VARS.Thi
svari
abl
eisus
edwhi
leacces
singcooki
es.

 Val
ue 
−Thi
ssetstheval
ueofthenamedvari
abl
eandi
sthecontentthatyouactual
lywant
tos
tore.

 Expi
ry 
−Thi
sspeci
fyafutureti
mei
nsecondss
ince00:
00:
00GMTon1s
tJan1970.After
thi
sti
mecooki
ewi
llbecomei
nacces
sibl
e.I
fthi
sparameteri
snot s
et then cooki
ewi
ll
automati
cal
lyexpi
rewhentheWebBrows
eri
scl
osed.

 Path 
−Thi
sspeci
fiesthedi
rectori
esforwhi
chthecooki
eisval
id.A s
ingl
eforwards
las
h
characterpermi
tsthecook
ietobeval
idforal
ldi
rectori
es.
 Domai
n − Thi
scan be us
ed to s
peci
fythe domai
n name i
n veryl
arge domai
nsand mus
t
contai
natl
eas
ttwoper
iodstobeval
id.Al
lcook
iesareonl
yval
idforthehos
tanddomai
n
whi
chcreatedthem.

 Securi
ty 
−Thi
scanbes
etto1 tos
peci
fythatthecooki
eshoul
donl
ybes
entbys
ecure
trans
mis
sion us
ingHTTPSother
wis
esetto0 whi
ch mean cooki
ecan bes
entbyr
egul
ar
HTTP.

Foll
owing exampl
e wil
lcreate two cooki
es 
name 
and 
age 
thes
e cooki
eswi
llbe
expiredafteronehour.
<?php
setcooki
e("
name","
JohnWatkin"
,time()
+3600,"
/"
,"
",0)
;
setcooki
e("
age"
,"36",ti
me(
)+3600,"/
",""
,0);
?>
<html>

<head>
<titl
e>Setti
ngCooki
eswi
thPHP</
titl
e>
</head>

<body>
<?phpecho"
SetCooki
es"
?>
</body>

</
html
>

PHPs
etcooki
e()functi
on
PHPs
etcooki
e()functi
oni
sus
edtos
etcooki
ewi
thHTTPres
pons
e.Oncecooki
eiss
et,youcanacces
s
i
tby$_COOKI
Esupergl
obalvari
abl
e.

Syntax

1. bool
 s
etcooki
e (
 s
tri
ng 
$name 
[,
 s
tri
ng 
$val
ue 
[,
 i
nt 
$expi
re 

0 [

stri
ng 
$path 
 
 
2. [

stri
ng 
$domai
n [

bool
 $s
ecure 

fal
se 
[,
 bool
 $httponl
y =
 fal
se 
]]
]]
]]
 )
  

Exampl
e

1. s
etcooki
e("
Cooki
eName"

"Cooki
eVal
ue"
);
/* 
defi
ning 
name 
and 
val
ue 
onl
y*/
  
2. s
etcooki
e("
Cooki
eName"

"Cooki
eVal
ue"

time(
)+1*
60*
60)
;/
/us
ing 
expi
ry 
in 
1 hour(
1*60*
60 
seconds
 or 
3600 
s
econds

 
3. s
etcooki
e("
Cooki
eName"

"Cooki
eVal
ue"

time(
)+1*
60*
60,
 "
/mypath/
",
 "
mydomai
n.com"

1);
  

Ac
ces
singCo
oki
eswi
thPHP
PHP provi
des many ways to acces
s cooki
es. Si
mpl
est way i
s to us
e ei
ther $_COOKI
E or
$HTTP_COOKI
E_VARSvari
abl
es.Fol
lowi
ngexampl
ewi
llacces
sal
lthecooki
ess
eti
naboveexampl
e.
4. <html
>
5. <head>
6. <title>Acces
singCooki
eswi thPHP</
titl
e>
7. </ head>
8. <body>
9. <?php
10. echo$_COOKI E["
name"]
."<br/>
";

11.

12. /
*isequi
val
entto*
/

13. echo$HTTP_COOKI
E_VARS[
"name"
]."
<br/
>";

14.

15. echo$_COOKI
E["
age"
]."
<br/
>";

16.

17. /
*isequi
val
entto*
/

18. echo$HTTP_COOKI
E_VARS[
"age"
]."
<br/
>";

19. ?>

20.

21. </
body>

22.</
html
>

PHPCooki
eExampl
e
Fi
le:cooki
e1.
php
1. <?php 
 
2. s
etcooki
e("
user"

"Sonoo"
);
  
3. ?> 
 
4. <html
>  
5. <body> 
 
6. <?php 
 
7. i
f(!
iss
et(
$_COOKI
E["
user"
])


 
8.  
 
  
echo 
"Sorry,
 cooki
e i
s not 
found!
";
  
9. }
 el
se 

 
10. 
 
  
echo 
"<br/
>Cooki
e Val
ue:
 "
 .
 $_COOKI
E["
user
"];
  
11.}
  
12.?> 
 
13.</
body> 
 
14.</
html
>  

15. Youcanus
e i
sset(

functi
ontochecki
facooki
eiss
etornot.

16.<html
>
17.

18. <head>

19. <ti
tle>Acces
singCooki
eswi
thPHP</
titl
e>

20. </
head>

21.

22. <body>

23.

24. <?php

25. i
f(i
sset(
$_COOKI
E["
name"
])
)

26. echo"
Wel
come".$_COOKI
E["
name"
]."
<br/
>";

27.

28. el
se

29. echo"
Sorry.
..Notrecogni
zed"."
<br/
>";

30. ?>

31.

32. </
body>

33.</
html
>

De
leti
ngCo
oki
ewi
thPHP
Offi
cial
ly,todel
eteacooki
eyous
houl
dcal
lsetcooki
e()wi
ththenameargumentonl
ybutthi
sdoes
notal
waysworkwel
l,however,ands
houl
dnotberel
iedon.

I
tiss
afes
ttos
etthecooki
ewi
thadatethathasal
readyexpi
red−

34.<?php

35. s
etcooki
e("
name"
,""
,ti
me(
)-60,"
/"
,"
",0)
;

36. s
etcooki
e("
age"
,""
,ti
me(
)-60,"
/"
,"
",0)
;

37.?>

38.<html
>

39.

40. <head>

41. <ti
tle>Del
eti
ngCooki
eswi
thPHP</
titl
e>
42. </
head>

43.

44. <body>

45. <?phpecho"
Del
etedCooki
es"?>

46. </
body>

47.

48.</
html
>

PHPSes
sion
PHPsess
ioni sus
edtos
toreandpas
sinformati
onfromonepagetoanothertemporari
ly(
unti
lus
ercl
ose
thewebsite)
.

PHPs ess
iontechni
quei swidelyusedinshoppingwebsi
teswhereweneedtostoreandpas
scart
informatione.
g.username,productcode,productname,pr
oductpri
ceetcfromonepagetoanother.

PHPsess
ioncreatesuni
queus
eri
dforeachbrows
ertorecogni
zetheus
erandavoi
dconfl
ictbetween
mul
tipl
ebrowsers.

PHPs
ess
ion_s
tart(
)functi
on
PHPs es
sion_s
tart(
)functi
onisusedtostartthes ess
ion.Itstartsaneworresumesexisti
ngses
sion.It
returnsexis
tingsess
ionifses
sioni
screatedalready.Ifsess
ionisnotavai
labl
e,itcreatesandreturns
news es
sion.

Syntax

1. bool
 s
ess
ion_
start 

voi
d )
  
Exampl
e

1. s
ess
ion_s
tar
t()

 

PHP$_SESSI
ON
PHP$_SESSIONisanassoci
ati
vearraythatcontai
nsal
lses
sionvari
abl
es.I
tisus
edtos
etandget
ses
sionvari
abl
evalues
.

Exampl
e:Storei
nformati
on

1. $_
SESSI
ON[
"us
er"


"Sachi
n";
  

Exampl
e:Geti
nformati
on

1. echo 
$_SESSI
ON[
"us
er"
];
  

PHPSes
sionExampl
e
Fi
le:s
ess
ion1.
php
1. <?php 
 
2. s
ess
ion_s
tar
t()

 
3. ?> 
 
4. <html
>  
5. <body> 
 
6. <?php 
 
7. $_
SESSI
ON[
"us
er"


"Sachi
n";
  
8. echo 
"Ses
sion 
informati
on 
are 
set 
succes
sful
ly.
<br/
>";
  
9. ?> 
 
10.<a 
href="
ses
sion2.
php"
>Vi
sit 
next 
page</
a> 
 
11.</
body> 
 
12.</
html
>  
Fi
le:s
ess
ion2.
php
1. <?php 
 
2. s
ess
ion_s
tar
t()

 
3. ?> 
 
4. <html
>  
5. <body> 
 
6. <?php 
 
7. echo 
"Us
er 
is:
 "
.$_SESSI
ON[
"us
er"
];
  
8. ?> 
 
9. </
body> 
 
10.</
html
>  

next→← pr
ev

PHPSes
sionCounterExampl
e
Fi
le:s
ess
ioncounter.
php
1. <?php 
 
2.  
 
 ses
sion_s
tart(
);
  
3.  
 
4.  
 
 if 
(!
iss
et(
$_SESSI
ON[
'
counter'
]
)) 

 
5.  
 
  
  
$_SESSI
ON[
'
counter'
]
 = 
1; 
 
6.  
 
 } 
els
e {
  
7.  
 
  
  
$_SESSI
ON[
'
counter'
]
++;
  
8.  
 
 } 
 
9.  
 
 echo 
("
Page 
Views

".
$_SESSI
ON[
'
counter'
]
); 
 
10.?> 
 

PHPDes
troyi
ngSes
sion
PHPs
ess
ion_des
troy(
)functi
oni
sus
edtodes
troyal
lses
sionvari
abl
escompl
etel
y.

Fi
le:s
ess
ion3.
php
1. <?php 
 
2. s
ess
ion_s
tart(
);
  
3. s
ess
ion_des
troy(
);
  
4. ?> 
 

PHPFi
leHandl
ing
PHPFileSystemal
lowsustocreatefil
e,r
eadfi
lel
inebyl
ine,readfi
lecharacterbycharacter,wri
tefi
le,
appendfi
le,del
etefil
eandclosefil
e.

PHPOpenFi
le-fopen(
)
ThePHPfopen(
)functi
oni
sus
edtoopenafi
le.

Syntax

1. res
ource 
fopen 

stri
ng 
$fi
lename 

stri
ng 
$mode 
[,
 bool
 $us
e_i
ncl
ude_path 

fal
se 
[,
 res
ource 
$context 
]]
 )
  

Exampl
e

1. <?php 
 
2. $handl
e = 
fopen(
"c:
\\fol
der\\fi
le.
txt"

"r"
);
  
3. ?> 
 

PHPCl
oseFi
le-fcl
ose(
)
ThePHPfcl
ose(
)functi
oni
sus
edtocl
oseanopenfi
lepoi
nter.

Syntax
1. ool
 fcl
ose 

res
ource 
$handl
e )
  

Exampl
e

1. <?php 
 
2. fcl
ose(
$handl
e);
  
3. ?> 
 

PHPReadFi
le-fread(
)
ThePHPfread(
)functi
oni
sus
edtoreadthecontentofthefi
le.I
tacceptstwoarguments
:res
ourceand
fi
lesi
ze.

Syntax

1. s
tri
ng 
fread 

res
ource 
$handl
e ,
 i
nt 
$length 

 

Exampl
e

1. <?php 
 
  
2. $fi
lename 

"c:
\\myfi
le.
txt"

  
 
3. $handl
e = 
fopen(
$fi
lename,
 "
r")
;/
/open 
fil
e i
n read 
mode 
 
  
4.  
 
5. $contents
 = 
fread(
$handl
e, 
fil
esi
ze(
$fi
lename)
);
//
read 
fil
e  
 
 
6.  
 
7. echo 
$contents
;//
pri
nti
ng 
data 
of 
fil
e  
8. fcl
ose(
$handl
e);
//
clos
e fi
le 
 
  
9. ?> 
 
  

PHPWri
teFi
le-fwri
te(
)
ThePHPfwr
ite(
)functi
oni
sus
edtowri
tecontentofthes
tri
ngi
ntofi
le.

Syntax

1. i
nt 
fwri
te 

res
ource 
$handl
e ,
 s
tri
ng 
$stri
ng 
[,
 i
nt 
$length 


 

Exampl
e

1. <?php 
 
2. $fp 

fopen(
'
data.
txt'
,
 '
w'
);
//
open 
fil
e i
n wri
te 
mode 
 
3. fwri
te(
$fp,
 '
hel
lo 
'
); 
 
4. fwri
te(
$fp,
 '
php 
fil
e'
);
  
5. fcl
ose(
$fp)

 
6.  
 
7. echo 
"Fi
le 
wri
tten 
succes
sful
ly"

 
8. ?> 
 
PHPDel
eteFi
le-unl
ink
()
ThePHPunl
ink(
)functi
oni
sus
edtodel
etefi
le.

Syntax

1. bool
 unl
ink 

stri
ng 
$fi
lename 
[,
 res
ource 
$context 


 

Exampl
e

1. <?php 
 
  
2. unl
ink(
'
data.
txt'
)
;  
3.  
 
 
4. echo 
"Fi
le 
del
eted 
succes
sful
ly"

 
5. ?> 
 

PHPOpenFi
le
PHPfopen()functioni
susedtoopenfi l
eorURLandreturnsres ource.Thefopen(
)functi
onacceptstwo
arguments
:$fi l
enameand$mode.The$fi lenamerepresentsthefiletobeopendedand$moderepresents
thefilemodeforexampleread-onl
y,read-write,wri
te-onlyetc.

Syntax

1. res
ource 
fopen 

stri
ng 
$fi
lename 

stri
ng 
$mode 
[,
 bool
 $us
e_i
ncl
ude_path 

fal
se 
[,
 res
ource 
$context 
]]
 )
  

PHPOpenFi
leMode

Mode Des
cri
pti
on

r Opensfi
lei
n read-onl
y mode.I
tpl
acesthefi
lepoi
nteratthebegi
nni
ngofthefi
le.

r+ Opensfi
lei
n read-wri
te 
mode.I
tpl
acesthefi
lepoi
nteratthebegi
nni
ngofthefi
le.

w Opensfi
lei
n wri
te-onl
y mode.I
tpl
acesthefi
lepoi
ntertothebegi
nni
ngofthefi
leandtruncatesthefi
let
fi
lei
snotfound,i
tcreatesanewfi
le.

w+ Opensfi
lei
n read-wri
te 
mode.I
tpl
acesthefi
lepoi
ntertothebegi
nni
ngofthefi
leandtruncatesthefi
let
fi
lei
snotfound,i
tcreatesanewfi
le.

a Opensfi
lei
n wri
te-onl
y mode.I
tpl
acesthefi
lepoi
ntertotheendofthefi
le.I
ffi
lei
snotfound,i
tcreate

a+ Opensfi
lei
n read-wri
te 
mode.I
tpl
acesthefi
lepoi
ntertotheendofthefi
le.I
ffi
lei
snotfound,i
tcreate

x Createsandopensfi
lei
n wri
te-onl
y mode.I
tpl
acesthefi
lepoi
nteratthebegi
nni
ngofthefi
le.I
ffi
lei
sfo
functi
onreturnsFALSE.
x+ I
tiss
ameasxbuti
tcreatesandopensfi
lei
n read-wri
te 
mode.

c Opensfi
lei
n wri
te-onl
y mode.I
fthefi
ledoesnotexi
st,i
tiscreated.I
fitexi
sts
,iti
snei
thertruncated(
a
northecal
ltothi
sfuncti
onfai
ls(
asi
sthecas
ewi
th'
x'
).Thefi
lepoi
nteri
spos
iti
onedonthebegi
nni
ngof

c+ I
tiss
ameascbuti
topensfi
lei
n read-wri
te 
mode.

next→← pr
ev

PHPOpenFi
le
PHPfopen(
)functi
oni
sus
edtoopenfi
leorURLandreturnsres
ource.Thefopen(
)functi
onacceptstwoarguments
$mode.The$fi
lenamerepres
entsthefi
letobeopendedand$moderepres
entsthefi
lemodeforexampl
eread-onl
y
-onl
yetc.

Syntax

1. res
ource 
fopen 

stri
ng 
$fi
lename 

stri
ng 
$mode 
[,
 bool
 $us
e_i
ncl
ude_path 

fal
se 
[,
 res
ource 
$context 
]]
 )
  

PHPOpenFi
leMode

Mode Des
cri
pti
on

r Opensfi
lei
n read-onl
y mode.I
tpl
acesthefi
lepoi
nteratthebegi
nni
ngofthefi
le.

r+ Opensfi
lei
n read-wri
te 
mode.I
tpl
acesthefi
lepoi
nteratthebegi
nni
ngofthefi
le.

w Opensfi
lei
n wri
te-onl
y mode.I
tpl
acesthefi
lepoi
ntertothebegi
nni
ngofthefi
leandtruncatest
l
ength.I
ffi
lei
snotfound,i
tcreatesanewfi
le.

w+ Opensfi
lei
n read-wri
te 
mode.I
tpl
acesthefi
lepoi
ntertothebegi
nni
ngofthefi
leandtruncatest
l
ength.I
ffi
lei
snotfound,i
tcreatesanewfi
le.

a Opensfi
lei
n wri
te-onl
y mode.I
tpl
acesthefi
lepoi
ntertotheendofthefi
le.I
ffi
lei
snotfound,i
fi
le.

a+ Opensfi
lei
n read-wri
te 
mode.I
tpl
acesthefi
lepoi
ntertotheendofthefi
le.I
ffi
lei
snotfound,i
fi
le.

x Createsandopensfi
lei
n wri
te-onl
y mode.I
tpl
acesthefi
lepoi
nteratthebegi
nni
ngofthefi
le.I
f
fopen(
)functi
onreturnsFALSE.

x+ I
tiss
ameasxbuti
tcreatesandopensfi
lei
n read-wri
te 
mode.
c Opensfi
lei
n wri
te-onl
y mode.I
fthefi
ledoesnotexi
st,i
tiscreated.I
fitexi
sts
,iti
snei
thertrun
to'
w'
),northecal
ltothi
sfuncti
onfai
ls(
asi
sthecas
ewi
th'
x'
).Thefi
lepoi
nteri
spos
iti
onedonth
thefi
le

c+ I
tiss
ameascbuti
topensfi
lei
n read-wri
te 
mode.

PHPOpenFi
leExampl
e
1. <?php 
 
2. $handl
e = 
fopen(
"c:
\\fol
der\\fi
le.
txt"

"r"
);
  
3. ?> 
 

PHPReadFi
le
PHPprovi
desvari
ousfuncti
onstoreaddatafromfile.Therearedi
fferentfuncti
onsthatal
lowyouto
readal
lfil
edata,readdatali
nebylineandreaddatacharacterbycharacter.

Theavai
labl
ePHPfi
lereadfuncti
onsaregi
venbel
ow.

o fread(
)
o fgets
()
o fgetc(
)

PHPReadFi
le-fread(
)
ThePHPfread(
)functi
oni
sus
edtoreaddataofthefi
le.I
trequi
restwoarguments
:fi
leres
ourceand
fi
lesi
ze.

Syntax

1. s
tri
ng 
fread 
(res
ource 
$handl
e ,
 i
nt 
$length 

 

$handl
e repres
entsfi
lepoi
nterthati
screatedbyfopen(
)functi
on.

$l
ength 
repres
entsl
engthofbytetober
ead.

Exampl
e
1. <?php 
 
  
2. $fi
lename 

"c:
\\fi
le1.
txt"

  
 
3. $fp 

fopen(
$fi
lename,
 "
r")
;/
/open 
fil
e i
n read 
mode 
 
  
4.  
 
5. $contents
 = 
fread(
$fp,
 fi
les
ize(
$fi
lename)
);
//
read 
fil
e  
 
 
6.  
 
7. echo 
"<pre>$contents
</pre>"
;/
/pri
nti
ng 
data 
of 
fil
e  
8. fcl
ose(
$fp)
;/
/cl
ose 
fil
e  
 
 
9. ?> 
 
  

Output

thi
sisfirstl
ine
thi
sisanotherli
ne
thi
sisthirdli
ne

PHPReadFi
le-fgets
()
ThePHPfgets
()functi
oni
sus
edtoreads
ingl
eli
nefromthefi
le.

Syntax

1. s
tri
ng 
fgets
 (
 res
ource 
$handl
e [

int 
$length 


 

Exampl
e
1. <?php 
 
  
2. $fp 

fopen(
"c:
\\fi
le1.
txt"

"r"
);
//
open 
fil
e i
n read 
mode 
 
  
3. echo 
fgets
($fp)

 
4. fcl
ose(
$fp)

 
5. ?> 
 
  

PHP-Re
gul
arExp
res
sio
ns
Regul
arexpres
sionsarenothi
ngmorethanas
equenceorpatternofcharactersi
tsel
f.Theyprovi
dethe
foundati
onforpattern-matchi
ngfuncti
onal
ity.

Us
ingregul
arexpres
sionyoucans
earchaparti
cul
ars
tri
ngi
nsi
deaanothers
tri
ng,youcanrepl
aceone
s
tri
ngbyanothers
tri
ngandyoucans
pli
tas
tri
ngi
ntomanychunks
.

PHP offersfuncti
onss
peci
fictotwos
etsofregul
arexpres
sion functi
ons
,each corres
pondi
ngtoa
certai
ntypeofregul
arexpres
sion.Youcanus
eanyofthembas
edonyourcomfort.

 POSI
XRegul
arExpres
sions

 PERLStyl
eRegul
arExpres
sions

POS
IXRe
gul
arEx
pre
ssi
ons

Thes
tructureofaPOSI
Xregul
arexpres
sioni
snotdi
ssi
mil
artothatofatypi
calari
thmeti
cexpres
sion:
vari
ousel
ements(
operators
)arecombi
nedtoformmorecompl
exexpres
sions
.

Thes
impl
estregul
arexpres
sioni
sonethatmatchesas
ingl
echaracter,s
uchasg,i
nsi
des
tri
ngss
uchasg,
haggl
e,orbag.
Letsgi
ve expl
anati
on forfew conceptsbei
ng us
ed i
n POSI
X regul
arexpres
sion.Afterthat we wi
ll
i
ntroduceyouwi
thregul
arexpres
sionrel
atedfuncti
ons
.

Brackets

Brackets(
[]
)haveas
peci
almeani
ngwhenus
edi
nthecontextofregul
arexpres
sions
.Theyareus
edto
fi
ndarangeofcharacters
.

Sr.
N Expres
sion&Des
cri
pti
on
o

1 [
0-9]
I
tmatchesanydeci
maldi
gitfrom0through9.

2 [
a-z
]
It matches any character from l
ower-cas
ea
throughlowercas
ez.

3 [
A-Z]
It matches any character from uppercas
eA
throughuppercas
eZ.

4 [
a-Z]
It matches any character from l
owercas
ea
throughuppercas
eZ.

Therangess
hownabovearegeneral
;youcoul
dal
sous
etherange[
0-3]tomatchanydeci
maldi
git
rangi
ngfrom 0 through3,ortherange[
b-v]tomatchanyl
owercas
echaracterrangi
ngfrom b
thr
oughv.

Quanti
fiers
Thefrequencyorpos
iti
onofbracketedcharacters
equencesands
ingl
echaracter
scanbedenoted
byas
peci
alcharacter.Each s
peci
alcharacterhavi
ngas
peci
ficconnotati
on.The+
,*,?,{
int.
range}
,and$fl
agsal
lfol
lowacharacters
equence.

Sr.
N Expres
sion&Des
cri
pti
on
o
1 p+
I
tmatchesanys
tri
ngcontai
ningatl
eas
tonep.

2 p*
Itmatchesanys
tri
ngcontai
ningz
eroormore
p'
s.

3 p?
I
tmatchesanys
tri
ngcontai
ningz
erooronep'
s.

4 p{
N}
It matches any s
tri
ng contai
ning a s
equence
of 
N p'
s

5 p{
2,3}
Itmatchesanystri
ngcontai
ningas
equenceof
twoorthreep'
s.

6 p{
2,}
Itmatchesanystri
ngcontai
ningas
equenceof
atl
easttwop's
.

7 p$
I
tmatchesanys
tri
ngwi
thpattheendofi
t.

8 ^
p
I
tmatchesanys
tri
ngwi
thpatthebegi
nni
ngof
i
t.

Exampl
es
Fol
lowi
ngexampl
eswi
llcl
earyourconceptsaboutmatchi
ngcharacters
.

Sr.
N Expres
sion&Des
cri
pti
on
o
1 [
^a-z
A-Z]
Itmatchesanys tri
ngnotcontai ni
nganyofthe
characters ranging from a through z and A
throughZ.

2 p.
p
Itmatchesanys tri
ngcontaini
ngp,followedby
anycharacter,i
nturnfol
lowedbyanotherp.

3 ^
.{2}
$
It matchesany s
tri
ng contai
ning exactl
y two
characters
.

4 <b>(
.*)
</b>
It matchesanys
tri
ngencl
osed wi
thi
n <b>and
</
b>.

5 p(
hp)
*
I
tmatchesanystri
ngcontai
ningapfol lowedby
z
eroormoreins
tancesofthesequencephp.

Predefi
nedCharacterRanges
Foryourprogrammi
ngconveni
ences
everalpredefi
nedcharacterranges
,al
sok
nownascharacter
cl
ass
es,ar
eavai
labl
e.Charactercl
ass
ess
peci
fyanenti
rerangeofcharacters
,forexampl
e,the
al
phabetorani
ntegers
et−

Sr.
N Expres
sion&Des
cri
pti
on
o

1 [
[:
alpha:
]]
It matches any stri
ng contai
ning al
phabeti
c
charactersaAthroughzZ.

2 [
[:
digi
t:]
]
It matches any s
tri
ng contai
ning numeri
cal
digi
ts0through9.

3 [
[:
alnum:
]]
It matchesanys tri
ngcontai
ningal
phanumeri
c
charactersaAthroughz
Zand0through9.

4 [
[:
space:
]]
I
tmatchesanys
tri
ngcontai
ningas
pace.

PHP'
sRe
gexpPOS
IXFunc
tio
ns
PHPcurrentl
yofferss
evenfuncti
onsfors
earchi
ngs
tri
ngsus
ingPOSI
X-s
tyl
eregul
arexpres
sions−

Sr.
N Functi
on&Des
cri
pti
on
o

1 ereg(
)

Theereg()functionsearchesas trings peci


fied
by stri
ng for a string speci
fied by pattern,
returni
ng true if the pattern is found,and
fals
eotherwise.

2 ereg_repl
ace(
)

Theereg_replace(
)functi
onsearchesforstri
ng
specifiedbypatternandrepl
acespatternwith
replacementi
ffound.

3 eregi
()

The eregi() function searches throughout a


string speci
fied by pattern for a s tri
ng
specifi
ed by string. The s earch i
s not case
s
ens
iti
ve.

4 eregi
_repl
ace(
)

The eregi_replace(
)function operates exactly
li
keereg_r eplace(
),except that thesearch for
patternins tringisnotcases ens
i ti
ve.

5 s
pli
t()

The s pl
it(
)function wi
lldivi
de a stri
ng i
nto
various elements
, the boundaries of each
elementbas edontheoccurrenceofpatternin
string.

6 s
pli
ti(
)

The spl
iti
()function operates exactl
yin the
samemannerasi tssi
blingspli
t()
,exceptthatit
isnotcasesens
itive.

7 s
ql_regcas
e()

Thes ql_regcas
e()functioncanbethoughtofas
auti l
ityfuncti on,convertingeachcharacteri n
the input parameter s tring into a bracketed
expressioncontainingtwocharacters .

You might also like