Professional Documents
Culture Documents
Introduction
PH P is a ge ne ral -purpos e s cripting l
anguage e s pe cial l
y s uite d
for W e b de ve lopm e nt. Itis ope n-s ource and can be de pl oye d
on al lm ajor ope rating s ys te m s and w e b s e rve rs fre e ofch arge .
Itis im pe rative , re fl
e ctive and s upports obje ct-orie nte d
program m ing.
Bas ic Syntax
Sim il
aritie s w ith oth e r im pe rative l
anguage s
Variabl
es
$foo = 'bar';
Note th atPH P doe s notby de faul te nforce th e de claration ofa
variabl e be fore its us age , s o s om e care s h oul d be tak e n notto
atte m ptto acce s s th e value ofa variabl e be fore one h as be e n
as s igne d to it.
Es caping H TM L
<html>
<body>
<?php $lastname = "bar"; ?>
Hello, foo<?php echo $lastname; ?>!
</body>
</html>
Type s
PH P s upports th e fol
low ing e igh tprim itive data type s .
Bool
e ans
Th e bool
e an type can tak e th e val
ue s true or fal
s e . Since PH P
is w e ak ly-type d, variabl e s ofoth e r type s can be im plicitl
y cas t
as bool e ans . H ow e ve r, one m us tbe care fulw h e n doing th is ,
be caus e s om e val ue s ofoth e r type s are e val uate d to FALSE,
as lis te d be l
ow .
Inte ge rs and fl
oats
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
Strings
Arrays
$arr=array("key1"=>"val1","key2"=>"val2","val
3");
O bje cts
//class Declaration
class className [extends parentClassname] {
var $attribute;
//constructor
function className([$params]){statement;}
//member function
function functionName([$params]){statements;}
}
//class instantiation
$ob = new className();
//calling a member function
$result = $ob->functionName();
Type functions
Us e fulSupe rgl
obal
s
Se s s ion H andl
ing
PH P h as l
ibrarie s for inte rfacing w ith m os tofth e popul ar
re l
ationaldatabas e s e rve rs in us e today. Th e s e incl ude
M ySQL, Pos tgre SQL, O racl e and M S SQL. Itis be yond th e
s cope ofth is prim e r to e xh aus tive l
y e xpl
ore th e rich l
ibrarie s of
functions provide d by PH P to acce s s th e s e databas e s .
if-e l
s e if-e l
se:
if (condition){ statement;}
//yes, elseif is one word
elseif(condition){ statement; }
else{ statement; }
s w itch cas e :
switch (expression){
case constant1: statement; break;
case constant2: statement; break;
default: statement; break;
}
Ite ration
w h il
e l
oop:
while(condition){ statement; }
for l
oop:
for(init expr; terminate expr; iterate
expr){ statement; }
foreach loop:
foreach($array as $val){ statement; }
or
foreach($array as $key=>$val){ statement; }
O pe rators (de cre as ing pre ce de nce )
PH P Prim e r v0.1
Cre ate d by: Adrian Que k and Ch in Yong, Singapore PH P
Us e r Group (w w w .ph p.com .s g)
Copyrigh t: 22 Fe b 2008 GNU FDL,
(w w w .gnu.org/copyl e ft/fdl
.h tm l
)