lap trinh php va my sql

Download Lap trinh php va my sql

Post on 12-Nov-2014

637 views

Category:

Technology

4 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

n);} 5 6. Ban ch o cntt ca c quan ngPHP & MySQL b. Cc hng s c nh ngha trong PHP (Built-in Constants): h tr cho ngi lp trnh, PHP cung cp sn cc hng s nh : ccbin mi trng, cc bin ca Web server Apache ... Ngi lp trnh c th sdng hm phpinfo() xem cc gi tr ny. + Hng s nguyn : y l nhng gi tr c kiu integer. V d : 10+ Hng s thc: y l nhng gi tr c kiu double. V d : 10.00+ Hng k t : y l mt xu k t t trong du ngoc n hockp.V d : Ngn ng lp trnh PHP.6 7. Ban ch o cntt ca c quan ngPHP & MySQL3. Bin v gi tr logic.+ Cng ging vi C/C++, PHP khng c khi nim TRUE v FALSE.Cc gi tr TRUE c hiu l nhng gi tr bng 1 v gi tr FALSE lnhng gi tr bng 0 hoc xu rng .+ Khi s dng bin chng ta khng cn khai bo kiu .V d : $a = 1; // $a l mt bin kiu integer.$a = 1.2; // by gi $a l mt bin kiu double.$a = A ; // by gi $a li l mt bin kiu string. + Nu nh thc hin php ton gia bin c kiu s v kiu string,PHP s coi chui l mt dy s nh sau :$str = 222B Baker Street;Ta thy bin $str c gi tr kiu string, v nu cng s 3 vi gi tr nyth :$x = 3 + $str ; // $x = 225 khi bin $x nhn c gi tr 255 v PHP cng 3 vi ba s u.Nhng nu ta in gi tr ca bin $str thecho ($str);// print : 222B Baker StreetCh rng cc php ton gia s v chui ch ng khi k t u cachui l s .+ Ta cng c th lm thay i kiu gi tr ca mt bin bng cch pkiu$a = 11.2;// bin $a c kiu double$a = (int) $a;// by gi $a c kiu integer ,gi tr l 11$a = (double) $a; // by gi $a li c kiu double, gi tr l 11.0$b = (string) $a ; // bin $b c kiu string ,gi tr l 11 Cng phi bit rng PHP t ng chuyn i kiu rt tt. Nu tht scn thit chng ta mi phi dng cch trn. 7 8. Ban ch o cntt ca c quan ngPHP & MySQL+ Cc hm lm vic vi bin gettype() : hm ny tr li kiu ca mt bin no . Gi tr tr v cth l : integerdoublestringarrayobjectclassunknown typev d :if (gettype($user_input) == integer){ $age = $user_input;}settype() : hm ny p kiu cho mt bin no . Nu thnh cnghm tr v gi tr 1 (true) ,ngc li l 0 (false).v du :$a = 7.5;settype($a, integer);if (settype($a, array)){ echo (Conversion succeeded. );}else{ echo (Conversion error. );}isset() v unset() : Hm isset() kim tra mt bin c gn gi trhay cha, hm unset() s gii phng b nh cho mt bin no .v d : 8 9. Ban ch o cntt ca c quan ng PHP & MySQL$id = 323bb;if (isset($id)) { echo (D liu c gn);}else{ echo (D liu cha c gn);} unset($id); if(!isset($id)) {echo (D liu c gii phng);}empty() : Cng ging hm isset(), hm empty() s tr v gi tr 1(true) nu mt bin l rng v ngc li 0 (false). i vi bin c kiu sgi tr bng 0 c coi l rng, bin kiu string c coi l rng nu xu lxu rng.v d: echo empty($new) ; // true $new = 1; echo empty($new);// false $new = ; echo empty($new);// true $new = 0; echo empty($new);// true $new = So 323; echo empty($new);// false unset($new); echo empty($new);// true9 10. Ban ch o cntt ca c quan ng PHP & MySQLIII. Cc ton t + Bng cc php ton s hcPhp ton ngha V d Gii thch+Php cng7+2 Thc hin php cng gia 7 v 2 : 9 - Php tr 72Thc hin php tr gia 7 v 2 : 5 * Php nhn7*2Thc hin php nhn gia 7 v 2 : 14 / Php chia7/2Thc hin php chia gia 7 v 2 : 3.5%Chia d7%2 Thc hin php chia d gia 7 v 2 : 1 Ta c th vit cc php ton ngn gn nh bng sau :Khi vitTng ng vi$h += $i$h = $h + $i$h -= $i$h = $h - $i$h *= $i$h = $h * $i$h /= $i$h = $h / $i$h %= $i$h = $h % $i + Php gn : v d : $x = 1; $y = $x + 1; $length = $area / $width; 10 11. Ban ch o cntt ca c quan ngPHP & MySQL + Bng cc php ton quan hPhp nghaV d Gii thichton == So snh bng$h == Kim tra $h v $i c bng nhau< So snh nh hn $ikhng> So snh ln hn $h < $i Kim tra $h c nh hn $i khng $i Kim tra $h c ln hn $i khng >= bng$h Kim tra $h c khc $i khng $iSo snh khcKim tra $h c khc $i khngSo snh khc$h != $i$h $iCc php so snh thng dng kim tra iu kin trong cc cu lnhiu khin m ta s hc bi sau . + Bng cc php ton logic Php ton logic cng vi ton hng to thnh biu thc logic. Biu thc logic c th c gi tr l 1 (true) hoc 0 (false) . Ton hng a Ton hng b a && ba || b !a !b1 11 1 0 01 00 1 0 10 10 1 1 00 00 0 1 1 + Cc php ton vi bin kiu string .11 12. Ban ch o cntt ca c quan ngPHP & MySQL Ta s dng du . ghp hai bin kiu string vi nhau . v d : $first = Phineas;$last = Phop;$full = $first. . $last; // $full = Phineas Phop ;echo ($full);Ta c th ghp hai xu nh sau:echo ($last. s Bicycles);//print : Phops Bicycles c th chn mt bin vo trong hng c kiu string th tn binphi trong du ng m ngoc nhn.echo (${last}s Bicycles); + Cc php ton thao tc mc bit. Cc php ton thao tc mc bit tc ng ln tng bit ca ton hng .K hiu ngha& AND bit|OR bit^ XOR bit Bng cc php ton nh sau :&Kt qu| Kt qu ^ Kt qu 1&1 11|1 11^10 1&0 01|0 11^01 0&1 00|1 10^11 0&0 00|0 00^00 PHP cng h tr cc php dch phi v dch tri >> : dch phi > n lm cho cc bittrong $a b dch phi i n v tr. Tng t ta c php dch tri .12 13. Ban ch o cntt ca c quan ng PHP & MySQLv d : 11 >> 2 = 2 ;v :11 (1011)>> 22 (0010)+ Cc php ton tng gim :- Php tng : php tng (ton t tng) tng gi tr ca ton hng lnmt n v.$a ++ : $a c s dng ri mi tng++ $a : $a tng ri mi c s dng- Php gim : tng t nh php tng, khc l gi tr b gim i mtn v.$a -- : $a c s dng ri mi gim-- $a : $a gim ri mi c s dngv d :$a = 10; // $a bng 10$b = $a++ ; // $a bng 11 nhng $b bng 10$a = 10; // $a bng 10$b = -- $a ; // $a bng 9 v $b bng 9+ Php ton iu kin. ? :Php ton iu kin cng vi ton hng to nn biu thc iu kin.Ta k hiu e1 ,e2, e3 l ba ton hng.Biu thc c dng : e1 ? e2 : e3Nu e1 != 0 th gi tr ca biu thc iu kin l e2Nu e1 == 0 th gi tr ca biu thc iu kin l e3v d : tm maxmax = $a>$b ? a : b ; 13 14. Ban ch o cntt ca c quan ngPHP & MySQL+ Ton t sizeof (i tng) :Php ton sizeof cho bit kch thc (tnh bng byte ) nh m itng chim trong b nh. i tng y c kiu l integer, double,string.v d : $a = 10;echo sizeof($a); //s in ra mn hnh l : 4IV. Cc cu lnh iu khin1. Lnh if_else : y l lnh r nhnh c iu kin .a. Dng 1 :if ( biu thc ) cu lnh; Cu lnh y tng ng vi mt khi lnh. Mt khi lnh ct trong du ngoc kp. ngha :+ Nu biu thc khc khng ,th cu lnh c thc hin.+ Nu biu thc bng khng, th cu lnh khng c thc hinb. Dng 2 :if ( biu thc ) cu lnh 1;else cu lnh 2; ngha :+ Nu biu thc khc khng ,th cu lnh 1 c thc hin.+ Nu biu thc bng khng, th cu lnh 2 c thc hinCh :* Cu lnh 1 dng 2 l lnh if_else14 15. Ban ch o cntt ca c quan ngPHP & MySQL + Nu lng else bng lng If th else thuc v If gn nhttheo tng cp t trong ra ngoi. V d :$a = 10; $b = 10;$c = 3; $d = 3;$e = 12; $f = 8;if($a == $b)if($c == $d)if($e == $f)$max = $e;else$max = $f;else$max = $d;else$max = $becho $max ; //printf max = 8 + Nu lng else t hn lng If th else thuc v If ngn nhttheo tng cp t trong ra ngoi.V d :Tng ng vi :* Cu lnh 2 ca dng 2 l elseif :15 16. Ban ch o cntt ca c quan ngPHP & MySQL Bt ngun t :C th vit li nh sau: if ( biu_thc1 ) if ( biu_thc1 )cu_lnh 1;cu_lnh 1; elseelseif (biu_thc 2)if ( biu_thc 2 )cu_lnh 2; cu_lnh 2; elseif (biu_thc 3)elsecu_lnh 3; if ( biu_thc 3 ) ...cu_lnh 3;elseif (biu_thc i) ...cu_lnh i; else ...if ( biu_thc i ) elsecu_lnh i; cu_lnh n;...else cu_lnh n; Cu lnh elseif to ra lnh r nhnh c iu kin trong thc hin 1 trong n cch khc nhau. - Nu biu_thc i khc khng (i = 1,..n-1) th thc hin culnh i .- Nu biu_thc i bng khng (i = 1, ..n-1) th cu lnh thn c thc hin. 2. Cu lnh switch :switch (biu_thc n){ case n1:cu lnh 1;break; case n2:cu lnh 2;break; ... case nn:cu lnh nn;[default: cu lnh]} 16 17. Ban ch o cntt ca c quan ng PHP & MySQLCu lnh switch l cu lnh r nhnh c iu kin trong thchin mt trong n ci r nhnh.+Nu biu_thc n = ni (i = 1, .. n) th cu lnh sau case ni cthc hin cho n khi gp lnh break hoc du ngoc }+Nu biu_thc n != mi ni (i = 1, .. n) m c nhnh Default thcc cu lnh sau Default c thc hin.V d : 3. Cu lnh FOR: Dng lnh :for (biu_thc 1; biu_thc 2; biu_thc 3) cu lnh ; Lnh for l lnh to chu trnh c iu kin. Bc 1 : tnh biu_thc 1 Bc 2 : tnh biu_thc 2 :Nu biu_thc 2 khc 0 th thc hin cu lnh v sang bc 3.17 18. Ban ch o cntt ca c quan ngPHP & MySQLNu biu_thc 2 bng 0 th kt thc vng forBc 3 : tnh biu_thc 3 v quay li bc 2.+ biu_thc 1, biu_thc 2, biu_thc 3 l cc thnh phn. Mi thnhphn c th gm nhiu biu thc. Khi mi biu thc c vit cch nhaumt du phy (,).+Cc biu thc c tnh ln lt t tri qua phi+Biu thc trong biu_thc 2 quyt nh thc hin thn ca for.V d : +C th vng mt bt k thnh phn no. Nu vng mt biu_thc 2th cu lnh lun c thc hin. Mc d vng mt vn phi c du chmphy (;)V d : +Nu vng biu_thc 1 v biu_thc 3 th :for ( ; biu_thc 2 ; ) cau_lenh ;tng ng vi :while (biu_thc 2) cau_lenh ;4. Cu lnh WHILEwhile (biu_thc) cu lnh ;18 19. Ban ch o cntt ca c quan ng PHP & MySQLLnh while l mt lnh to chu trnh c iu kin. iu kin thc hinc kim tra u chu trnh.Bc 1 : Tnh biu thcNu biu thc khc khng, sang bc 2Nu biu thc bng khng, kt thc vng whileBc 2 : Thc hin cu lnh.Quay li bc 1.Ch : + Biu thc c th bao gm nhiu biu thc. Khi cc biu thc c vit cch nhau mt du phy ,v c tnh ln lt t tri qua phi. Biu thc cui cng quyt nh thc hin cu lnh.+ Khng c php vng mt biu thc+ to chu trnh v tn thwhile(1){... if (biu_thc) break; ...}V d :$i = 11;while (--$i){if (my_function($i) == error) { break; } ++ $number;}19 20. Ban ch o cntt ca c quan ngPHP & MySQL5. Lnh DO ... WHILEDng lnh : do {cu lnh; }while (biu_thc);Lnh do ... while l lnh to chu trnh c iu kin, trong iu kinthc hin chu trnh c kim tra cui chu trnh.Hot ng :Bc 1 : Thc hin cu lnhBc 2 : Tnh biu thc biu_thc +Nu biu thc biu_thc khc khng th quay li bc 1 +Nu biu thc biu_thc bng khng th kt thc do ... while.V d : 6. Lnh breakL lnh r nhnh khng iu kin v thng dng ra khi thn caswitch, while, do ... while, for .20 21. Ban ch o cntt ca c quan ng PHP & MySQLLnh break ch cho php thot khi thn cc lnh bn trong nht chan.7. Lnh continueL lnh r nhnh khng iu kin .Lnh thng dng bt u limt chu trnh mi trong cc lnh for, while, do ... while m khng cn thchin ht ton b thn ca ca lnh to chu trnh.8. Khai bo tin x l include v require . s dng cc on m bn ngoi, chng ta c th s dng khaibo tin x l include v require. Cho php chng ta xy dng cc hm cchng s, v bt k on m no sau c th chn vo cc on script.Require khc include l, n c th lm thay i ni dung ca trang hinti khi bin dch, cc trang ny dng khai bo cc bin, cc hng shay cc on m n gin khng c vng lp. Khi include ch