baitap php

Download Baitap PHP

Post on 13-Jul-2016

228 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

bai tap php

TRANSCRIPT

Sch bi tp PHPAiTi-Aptech Kin to tng laiTi liu lu hnh ni b

Bi tp PHP

Trung tm o to Lp trnh vin Quc t AiTi-AptechPage 1 of 32

35/115 Ph nh Cng - Hong Mai

Sch bi tp PHPAiTi-Aptech Kin to tng laiTi liu lu hnh ni b

Li gii thiu

Sch bi tp do tp th gio vin AiTi-Aptech thit k v c s dng nh mt phn khng th tch ri khi gio trnh ang hc ca Aptech n vi cc hc vin ang theo hc ti Trung tm.

Tp sch bi tp ny l ti liu lu hnh ni b, ch dnh cho cc hc vin theo hc ti Trung tm o to Lp trnh vin Quc t AiTi-Aptech. Mi hnh thc sao chp li ni dung ca sch l vi phm bn quyn v khng tun th Lut S hu tr tu ca Nh nc Vit Nam.

AiTi-Aptech lun mong mi to dng mt mi trng hc tp tt cho cc bn hc vin theo hc ti trung tm. Mi kin ng gp v xy dng Sch bi tp, ci tin h thng xin gi mail v contact.aiti@gmail.com hoc ng dy nng (04) 6 64 8848. Chng ti s ghi nhn v ci tin c th cung cp cho cc bn mt mi trng hc tp ngy mt tt hn.

S nghip tng lai ca cc bn l thnh cng ca chng ti

i thit k Sch bi tp

Vit Nam lun thiu Lp trnh vin ng cp Quc t

Trung tm o to Lp trnh vin Quc t AiTi-AptechPage 3 of 32

35/115 Ph nh Cng - Hong Mai

Ni dung

Ch ng 1 Gi i thi u PHP3

3

1. nh ngha3

2.Cch s d ng3

B. Bi t p3

C.Tham kh o3

Ch ng 2 X l Form trong PHP3

3

B. Bi t p3

C.Tham kh o4

Ch ng 3 S d ng bi n v bi u th c trong PHP4

4

B. Bi t p12

C.Tham kh o12

Ch ng 4 : Cu l nh i u khi n v vng l p trong PHP

12

A : Tm t t l thuy t12

B - Tham kh o14

C - Bi t p14

3- Dng vng l p gi i quy t bi ton : V a g v a ch,b l i cho trn, 36 con, 100 chn ch n?15

Ch ng 5 : S d ng hm trong PHP15

15

B.Tham kh o21

C.Bi t p21

Ch ng 6 : Lm vi c v i m ng trong PHP

21

21

B.Tham kh o24

C.Bi t p25

Sch bi tp PHPAiTi-Aptech Kin to tng laiTi liu lu hnh ni b

Ch ng 7 : Thao tc v i CSDL25

A.Bi t p

25

Ch ng 8 . Cookie v Session trong PHP27

A. Bi t p27

Ch ng 9 : Email v OOP29

A.Bi t p29

Chng 1 Gii thiu PHP

A.Tm tt l thuyt

1. nh ngha

PHP (vit tt hi quy "PHP: Hypertext Preprocessor") l mt ngn ng lp trnh kch bn hay mt loi m lnh ch yu c dng pht trin cc ng dng vit cho my ch, m ngun m, dng cho mc ch tng qut. N rt thch hp vi web v c th d dng nhng vo trang HTML. Do c ti u ha cho cc ng dng web, tc nhanh, nh gn, c php ging C v Java, d hc v thi gian xy dng sn phm tng i ngn hn so vi cc ngn ng khc nn PHP nhanh chng tr thnh mt ngn ng lp trnh web ph bin nht th gii.

2.Cch s dng

M php c lng vo m HTML

1.

2.

3.M mu

4.

5.

6.

9.

10.

Th s nh u s bt u v s kt thc ca phn m PHP qua my ch bit x l v dch m cho ng. y l mt im kh tin li ca PHP gip cho vic vit m PHP tr nn kh trc quan v d dng trong vic xy dng phn giao din ng dng HTTP.

Trung tm o to Lp trnh vin Quc t AiTi-AptechPage 5 of 32

35/115 Ph nh Cng - Hong Mai

Ngn ng, cc th vin, ti liu gc ca PHP c xy dng bi cng ng v c s ng gp khng nh ca cng ty Zend .

B. Bi tp

1.Ci t PHP v Apache web server bn mi nht ln my tnh c nhn

2. lu file sau vo th mc web ca apache vi tn vd1.php v chy th bng trnh duyt:

Testing page

C.Tham kho

1.Phpvietnam group http://groups.google.com/group/phpvietnam

2.Din n phpviet http://www.php vn.org

3.Chun vit m php http://pcdinh.googlepages.com/phpvietnamcodingstandards

Chng 2 X l Form trong PHP

A.Tm tt l thuyt

D liu ca ngi dng t trnh duyt s c gi ln my ch di dng tng cp bin=gi_tr v c th i theo 3 con ng khc nhau. Tu theo tng con ng c th, trn my ch ta cng c cc cch khc nhau ly d liu c gi ln.. 3 con ng l: GET, POST v COOKIES.

1. Truyn d liu thng qua phng thc GET

D liu gi t trnh duyt ln qua phng thc GET l phn d liu c nhp trc tip theo sau a ch efL do trnh duyt gi ln, c phn bit vi tn gile script bhng du hi chm ijk. V d, khi ta gl vo trnh duyt a ch efL sau:

http://mmm.phpvn.org/topic.phpjTOPIC_ID=1n1

Khi , trnh duyt s gi theo a ch trn mt cp bin = gi tr, trong bin c tn l TOPIC_ID

Sch bi tp PHPAiTi-Aptech Kin to tng laiTi liu lu hnh ni b

v gi tr l 1n1 iTOPIC_ID=1n1k.

Chng ta cng c th a ln nhiu cp bit=gi_tr bhng cch phn cch chng bi du o:

http://mmm.phpvn.org/index.phpjmethod=feplyoTOPIC_ID=1n1opOfeM_ID=qr

Vi a ch efL trn, chng ta s gi ln 3 cp bin=gi_tr theo phng thc GET, l: method=feply, TOPIC_ID=1n1 v pOfeM_ID=qr.

Khi trnh duyt gi cc thng tin ny ln my ch, PHP s t ng sinh ra mt mng c tn l

sHTTP_GET_VAfStu nvm gi tt c cc cp bin v gi tr , trong , ch s ca mng chnh l mt chuwi mang tn ca tn bin v gi tr ca ch s chnh l gi tr ca bin do trnh duyt gi ln. V d, vi a ch efL sau:

http://mmm.phpvn.org/post.phpjmethod=feplyoTOPIC_ID=1n1opOfeM_ID=qr

Th PHP s t ng sinh ra mt mng sHTTP_GET_VAfS c ni dung sau:

sHTTP_GET_VAfStxmethodxu = xfeplyx // tng ng vi cp method=feply sHTTP_GET_VAfStxTOPIC_IDxu = 1n1 // tng ng vi cp TOPIC_ID=1n1 sHTTP_GET_VAfStxpOfeM_IDxu = qr // tng ng vi cp pOfeM_ID=qr

Sau , trong trang meb ca mnh, cc bn c th tha hy s dng cc bin ny. V d, ti lm mt on chng trnh sau khi ngi dng nhp vo bin user=sinh th cho hin th xHello, my zossx, c{n nu bin user khc sinh th xHello x | gi tr ca bin:

}j

// Hm isset c s dng kim tra xem mt bin ~ c thit lp hay cha ig iisset isHTTP_GET_VAfStxuserxukk

ig isHTTP_GET_VAfStxuserxu==xsinhxk

echo xHello, my boss. Good morningx

else

echo xHello, x . sHTTP_GET_VAfStxuserxu . x. Good morningx

else

echo xHello, guest. Hom do you dojx

j

OK, by gi h~y save li. Gi s ti lu vi tn l melcome.php trong th mc mmm. M trnh duyt ln, gl vo Address d{ng ch sau:

http://localhost/melcome.phpjuser=sinh

Trung tm o to Lp trnh vin Quc t AiTi-AptechPage 7 of 32

35/115 Ph nh Cng - Hong Mai

H~y xem kt qu hin th trn mn hnh, sau thay ch sinh bhng mt ci tn g xem kt qu ra sao.

II. Phng thc POST

Post l phn d liu c gi qua cc gorm HTML c method =xPOSTx ixin xem li bi v HTMLk.

ly cc bin theo kiu POST, PHP s t ng sinh ra mng c tn l sHTTP_POST_VAfStu. Mng ny c ch s chnh l tn ca cc phn t trong gorm icc th input, select... c thuc tnh namek v gi tr l ni dung gi tr do ngi s dng nhp vo cc phn t c tn tng ng. Chng hn vi mu biu HTML sau:

}gorm method=xPOSTx }p

eser Name:}input type=xtextx name=xT1x sie=xqrx }/p }p

Passmord:

}input type=xpassmordx name=xTqx sie=xqrx}/p }pSex: }Select name =xsexx

}option value =1Male }/option

}option value =rpemale }/option }/select

}/p

}input type=xsubmitx value=xGui dix name=xz1x }/gorm

Khi ngi dng nhp user name igi s l Sinhk, passmord igi s l 1q35nk v chn sex l Male, khi , mng sHTTP_POST_VAfS s c cc phn t sau:

sHTTP_POST_VAfStxT1xu = Sinh

sHTTP_POST_VAfStxTqxu = 1q35n sHTTP_POST_VAfStxsexxu = 1

Sau khi ly c cc gi tr ny ryi, cc bn c th thoi mi s dng.

y l v d mt chng trnh gii phng trnh bc nht icho n n gin k }gorm method=xPOSTx

}p style=xmargin-top: r margin-bottom: rx

Nhp a:}input type=xtextx name=xax sie=xqrx}/p

}p style=xmargin-top: r margin-bottom: rxNhp b:}input type=xtextx name=xbx sie=xqrx}/ p

}p style=xmargin-top: r margin-bottom: rx

}input type=xsubmitx value=xTnhx name=xz1x}/p }/gorm

}jphp

sa=r sb=r

Sch bi tp PHPAiTi-Aptech Kin to tng lai

ig iisset isHTTP_POST_VAfStxaxukk

sa =sHTTP_POST_VAfStxaxu

ig iisset isHTTP_POST_VAfStxbxukk

sb =sHTTP_POST_VAfStxbxu

ig isa}rk

echo x}zfNghiem la: x . sb/saChw ny l -sb/sa

else

ig isb==rk

echo x}zfVo so nghiemx

else

echo x}zfVo nghiemx

j

B. Bi tp

1.Vit chng trnh cng 2 s c nhp t form

2.Vit trang login.php yu cu ngi dng nhp username v password

Ti liu lu hnh ni b

Xc nhn username l admin v password l 123456 th in ra dng Hello Admine Nu sai th yu cu fng nhp li .

C.Tham kho

1. http://www.w3schools.com/php/phpgforms.asp

Chng 3S dng bin v biu thc trong PHP

A.Tm tt l thuyt

KIU D LIU

PHP h tr 8 kiu d liu chnh:

4 kiu d liu v hng: boolean, integer, float (double), string.

Trung tm o to Lp trnh vin Quc t AiTi-AptechPage 9 of 32

35/115 Ph nh Cng - Hong Mai

2 kiu d liu t hp: array, object.

2 kiu d liu t bic: resource, NULL.

Kiu Boolean

Kiu boolean mang 1 trong 2 gi tr TRUE (ng) hoc FALSE (sai). V d:

"p" kiu sang boolean: mt s gi tr c chuyn i thnh FALSE trong cc biu thc boolean nu nh gi tr l:

s nguyn 0, s thc 0.0,

chui rng "", hoc chui "0",

mng rng (khng cha phn t no) Array(),

i tng khng cha phn t no (ch ng vi PHP4), gi tr NULL

Cc gi tr cn li s c chuyn i thnh TRUE.

Kiu Integer

Kiu integer mang cc gi tr s nguyn ..., -2, -1, 0, 1, 2, ...Trn hu ht cc h thng, kiu s nguyn c kch thc 32 bit, mang gi tr t -2147483647 cho n 2147483648. V d:

Kiu Float (Double)

Kiu float (hoc double) l kiu s thc, c th mang bt c gi tr s thc no. Trn hu ht cc h thng, kiu s thc c kch thc 64 bit. V d:

Kiu String

Kiu string lu gi 1 chui k t, mi k t c kch thc 1 byte. Ni dung string c t gia 2 du nhy, nhy n (') hoc nhy kp ("). V d

Khi s dng du nhy i bec chui, ngoi \', \" v \\, PHP c th nhhn dng thm mt s chui k t

escape t bic na:

\n: k t xung hng LF (