PHP Primer

I roduct nt ion
PH P is a ge ne ral -purpos e s cript l ing anguage e s pe cial s uit d l y e f W e b de v l e nt I is ope n-s ource and can be de pl d or e opm . t oye on alm aj ope rat s ys t m s and w e b s e rv rs f e ofch arge . l or ing e e re I is im pe rat e , re fe ct e and s upport obj ct t iv l iv s e -orie nt d e program m ing.

Bas ic Synt ax
Sim il ie s w it ot e r im pe rat e l arit h h iv anguage s PH P code s h oul l f il t anyone w h o h as h ad d ook am iar o e xpe rie nce w it C, C+ + or Jav St e m e nt are t rm inat d h a. at s e e w it a " " and bl s ofcode are de l it d w it " and " " Th e h ; ock im e h {" }. " *...*/ and " / not ions are bot v id f PH P Cont / " /" at h al or . rol s t ure s s uch as if le s t e m e nt , f l ruct -e s at s or oops and w h il l e oops w ork in m uch t e s am e w ay as w it t e abov m e nt h h h e ione d l anguage s . H ow e v r t e re are s om e dife re nce s and f at s t atare e , h f e ure h uniq ue t PH P w h ich w ilbe brie fy e xpl d in t e re s toft is o l l ore h h prim e r . V ariabls e V ariabls in PH P are re pre s e nt d by a " " ch aract r f l e d e e $ e ol ow by t e nam e oft e v h h ariabl. For e xam pl, t e v e e h ariabl " oobar" e f w oul be re pre s e nt d as " f d e $oobar" in PH P code . V ariabl e nam e s in PH P are cas e -s e ns it e and m us ts t w it e it e r an iv art h h unde rs core or a lte r f l e d by any num be r oflte rs , e t , ol ow e t num be rs or unde rs core s . Th e f l ing code s t ol ow ub de m ons t e s t e as s ignm e ntofa v ue t a v rat h al o ariabl. e $foo = 'bar';

Not t atPH P doe s notby de f te nf e h aul orce t e de cl ion ofa h arat v ariabl be f it us age , s o s om e care s h oul be t e n nott e ore s d ak o ate m ptt acce s s t e v ue ofa v t o h al ariabl be f one h as be e n e ore as s igne d t it o . Es caping H TM L PH P code can e m be dde d w it in H TM Lus ing t e " ph p" and h h <? " >" t . For e xam pl, t e f l ing code w oul out " e l ? ags e h ol ow d put H l o, f oobar!" . <html> <body> <?php $lastname = "bar"; ?> Hello, foo<?php echo $lastname; ?>! </body> </html>

Type s
PH P s upport t e f l ing e igh tprim it e dat t s . s h ol ow iv a ype f s cal t s : boolan, int ge r foat s t our ar ype e e , l , ring t o com pound t s : array, obj ct w ype e t o s pe cialype s : re s ource , NUL w t L PH P is a w e ak l ype d l y-t anguage . Th is m e ans t att e h h program m e r is us ual notre q uire d t re s ole t e t l y o v h ype ofa v ariabl. I t ad, t e t e ns e h ype ofa v ariabl is us ual re s ole d e l y v during runt e de pe nding on t e cont xtin w h ich itis be ing im h e us e d. Th is can s om e t e s be t y, as w e s h als e e . im rick l Boolans e Th e boolan t e ype can t e t e v ue s t or f s e . Since PH P ak h al rue al

is w e ak l ype d, v y-t ariabls ofot e r t s can be im pl l cas t e h ype icity as boolans . H ow e v r one m us tbe care f w h e n doing t is , e e , ul h be caus e s om e v ue s ofot e r t s are e v uat d t F SE, al h ype al e o AL as l t d be l . is e ow t e int ge r 0 and t e foat0.0 h e h l te st h ring " and e m pt s t 0" y rings an array w it z e ro e lm e nt h e s an obj ctw it z e ro m e m be r v e h ariabls (PH P 4 onl e y) t e NUL s pe cialype h L t

I e ge rs and foat nt l s I e ge rs can be s pe cif d in de cim al nt ie (10-bas e d), h e xade cim al (16-bas e d) or oct (8-bas e d) not ion, opt al at ional pre ce de d by l y a s ign (- or + ). O ct num be rs are pre ce de d by 0 and al h e xade cim al are pre ce de d by 0x. s Fl ing pointnum be rs can be s pe cif d in any oft e f l ing oat ie h ol ow t re e f at . h orm s $a = 1.234; $b = 1.2e3; $c = 7E-10; St rings Ast ring in PH P can be s pe cif d w it s ingl q uot s , doubl ie h e e e q uot s or t e h e re doc s ynt Since t is is a j ta prim e r t e e h ax. h us , h h e re doc s ynt w ilnotbe cov re d f t e s ak e ofbre v y. ax l e or h it Th e s im pls tw ay t s pe cif a s t e o y ring is t de l ititw it s ingl o im h e q uot s . T s pe cif a s ingl q uot w it in a s ingl-q uot d s t e o y e e h e e ring, itne e ds t be pre ce de d by a back s l h (\). I a back s l h is t o as f as o occur be f a s ingl q uot , t e n a doubl back s l h is ore e e h e as

re q uire d. O t e rw is e , t e re is norm al no ne e d t e s cape h h l y o back s l h e s . For e xam pl, t e code be l w oul print" is is as e h ow d Th a back s l h \, a s ingl q uot ' and a back s l h f l e d by a as e e as ol ow s ingl q uot \'" e e . echo 'This is a backslash \, a single quote \' and a backslash followed by a single quote \\''; For doubl-q uot d s t e e rings in PH P m ore oft e e s cape , h s e q ue nce s f ound in ot e r l h anguage s m ay be us e d, s uch as " , " " and " . H ow e v r t e m os tim port f at ofdoubl\n" \t \r" e , h ant e ure e q uot d s t e rings in PH P is t atv h ariabls are e xpande d. For e e xam pl, t e code be l w oul print" ariabl $oo h as a v ue e h ow d V e f al ofbar" . $foo = 'ba'.'r'; //'.' is the concatenation operator echo "Variable \$foo has a value of $foo"; Arrays I is be yond t e s cope oft is prim e r t f l de s cribe t e PH P t h h o ul y h array t , s o onl a brie fs um m ary w ilbe giv n. An array in ype y l e PH P is an orde re d m ap. A m ap is a t ype t atm aps v ue s t h al o k e ys . An array in PH P m ay be cre at d us ing t e array() e h f unct ion, as s h ow n be l . ow $arr=array("key1"=>"val1","key2"=>"val2","val 3"); Th e abov s t e m e ntw oul re s ulin an array w it t e e lm e nt e at d t h h e s v 1, v 2 and v 3. Since t e k e y ofv 3 w as nots pe cif d, it al al al h al ie w oul t e t e v ue 0, t e s m al s tav abl non-ne gat e d ak h al h l e ail e iv int ge r Al rnat e l t e abov array coul h av be e n e . t e iv y, h e d e

s pe cif d us ing t e f l ing s ynt ie h ol ow ax. $arr['key1'] = 'val1'; //$arr is created $arr['key2'] = 'val2'; $arr[] = 'val3'; //$arr[0] is equivalent L gal al s f k e ys ofarrays are any v ue s ofprim it e e v ue or al iv t s , w it boolan t and f s e be ing cas tas 1 and 0 ype h e rue al re s pe ct e l Fl ing pointnum be rs us e d as k e ys are iv y. oat t runcat d and NUL is cas tas an e m pt s t e L y ring. An e m pt y st ring, ofcours e , re m ains an e m pt s t y ring. O bj ct e s O bj ct in PH P l e arrays , is a v ry w ide t e s , ik e opic and h e nce w il l notbe cov re d be yond bas ic cl s de cl ion and obj ct e as arat e ins t iat s ynt ant ion ax. //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 f unct ions Som e us e f f ulunct ions f de al w it t s are t e or ing h ype h v dum p(), ge type () and is _ * f ar_ t unct ions . Th e v dum p ar_

f unct re curs iv l dum ps t e e nt v ion e y h ire ariabl pas s e d in, e incl uding t e t s oft e v h ype h ariabl and alit m e m be rs (f e ls or obj ct ) and e lm e nt (f arrays ). Th e ge type f e s e s or t unct re t ion urns te t h ype oft e v h ariabl pas s e d in and t e is _ * f e h unct ions t s tifa e v ariabl is ofa ce rt t . e ain ype For m ore inf at aboutPH P dat t s , plas e re f r t orm ion a ype e e o " tp:/ph p.ne tm anual n/anguage .t s .ph p" h t / / / l e ype .

Pos t Ge t Se s s ion and Dat , , abas e s
Since PH P is m os tcom m onl us e d t program w e b y o appl ions , itre q uire s w ays t obt dat f icat o ain a rom t e us e r h t rough H TTP PO ST and GET m e t ods as w e las w ays t h h l o m aint coh e re nts e s s ions f us e rs . Final m os tw e b ain or l y, appl ions re q uire w ays t s t us e r-s ubm ite d dat icat o ore t a. Us e f Supe rgl s ul obal AlPO ST v l ariabls m ay be obt e aine d f rom an array cal d l e $ PO ST and alGET v _ l ariabls f e rom t e $ GET array. Th e s e h _ v ariabls are cal d s upe rgl e l e obal ariabls , w h ich m e ans t at v e h t e y are aut at l gl . PH P prov s a f w ot e r us e f h om ical obal y ide e h ul s upe rgl s , incl obal uding $ CO O K I f cook ie s and $ FI ES f _ E or _ L or upl oade d f e s . il Se s s ion H andl ing T s t a s e s s ion, t e s e s s ion_ s t () f o art h art unct m us tbe cal d ion l e be f any ot e r out is s e ntt t e brow s e r afe r w h ich t e ore h put o h , t h $ SESSI N s upe rgl _ O obal ay be us e d t s t als e s s ion m o ore l v ariabls . Subs e q ue nty, alpage s t atf e l l h orm a partoft e h s e s s ion m us talo calt e s e s s ion_ s t () f s lh art unct ion. T e nd a o s e s s ion, t e s e s s ion_ de s t h roy() f unct is cal d. ion l e

H andl Dat ing abas e s PH P h as l ibrarie s f int rf or e acing w it m os toft e popul h h ar re l ional abas e s e rv rs in us e t at dat e oday. Th e s e incl ude M ySQL Pos t SQL O racl and M S SQL I is be yond t e , gre , e . t h s cope oft is prim e r t e xh aus t e l e xpl t e rich l h o iv y ore h ibrarie s of f unct ions prov d by PH P t acce s s t e s e dat ide o h abas e s . H ow e v r one oft e m os tus e f f at s aboutPH P is t atit e , h ule ure h t s t prov a m ore or ls s cons is t nts uit ofs im il yrie o ide e e e arl nam e d f unct ions t atare v id f m os tdat h al or abas e s . For ins t ance , t conne ctt a dat o o abas e s e rv r one w oul us e a e , d *_ conne ct f () unct ion, w h e re * is re pl d by t e t ace h ype ofs e rv r e . For a M ySQLs e rv r one w oul us e m ys q lconne ct f a M S e , d _ (), or SQLs e rv r one w oul us e m s s q lconne ct and s o on. e , d _ () Th e f unct ions us e d t q ue ry dat o abas e s are nots o cons is t nty e l nam e d, butafe r a q ue ry h as be e n e xe cut d, one w oul us e t e d t e *_ f t _ as s oc(), *_ f t _ array() and *_ f t _ row () h e ch e ch e ch f unct ions t re t v t e dat f o rie e h a rom t e q ue rie s . Final t cl e h l o os y, t e conne ct t t e dat h ion o h abas e , one w oul calt e *_ cl e () d lh os f unct ion.

Cont St ure s and O pe rat rol ruct ors
I m os tprogram m ing t orial, t e s e w oul be int n ut s h d roduce d e arl r in t e docum e nt H ow e v r s ince t is prim e r is prim aril ie h . e , h y m e antf program m e rs w h o are ne w t PH P and alo due t or o , s o t e f t att e s ynt oft e s e l h act h h ax h anguage cons t s in PH P ruct are s im il t t e ir count rpart in ot e r l ar o h e s h anguage s , w e h av e ch os e n prov onl brie fs um m arie s oft e m att e e nd oft is ide y h h h docum e nt .

Condit ional s if le if le : -e s -e s if (condition){ statement;} //yes, elseif is one word elseif(condition){ statement; } else{ statement; } s w it cas e : ch switch (expression){ case constant1: statement; break; case constant2: statement; break; default: statement; break; } I e rat t ion w h il l e oop: while(condition){ statement; } f l or 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 rat (de cre as ing pre ce de nce ) ors O pe rat ors new [] ++ -instanceof ! * / % + - . << >> < <= > >= == != === !== & ^ | && || ? : = += -= *= /= .= %= &= |= ^= <<= >>= and xor or Addit ionalnf at I orm ion ne w array() incre m e ntde cre m e nt / t s ype l ogical arit m e t h ic arit m e t and s t h ic ring bit is e w com paris on com paris on com paris on bit is e and re f re nce s w e bit is e w bit is e w l ogical l ogical t rnary e as s ignm e nt as s ignm e nt as s ignm e nt l ogical l ogical l ogical

PH P Prim e r v 0.1 Cre at d by: Adrian Que k and Ch in Yong, Singapore PH P e 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/ copylf / dl t l e tf .h m )
W it k ind s upportf h rom Ablw is e .com e

Sign up to vote on this title
UsefulNot useful