Professional Documents
Culture Documents
H Ch Minh
NI DUNG
Ph n 1: T ng quan v PHP Ph n 2: PHP c b n
Ni dung phn 1
1. Gi i thi u PHP
a. b. c. d. L ch s pht tri n PHP l g u i m M t s website dng PHP
2. Ho t ng c a Web Server
a. C ch b. V d
3. Yu c u ci t
a. Web server (Apache, IIS) b. Ph n m m PHP (trnh thng d ch PHP) c. H qu n tr c s d li u (mysql, SQL Server, Access)
H i u hnh UNIX (HP-UX, OpenBSD, Solaris, hnh: Linux), Mac OSX, Windows NT/98/2000/XP/2003/vista H QTCSDL Adabas D, dBase, Empress, FilePro QTCSDL: (read-only), Hyperwave, IBM DB2, Informix, Ingres, InterBase, FrontBase, mSQL, Direct MS-SQL, MySQL, ODBC, Oracle (OCI7 and OCI8), Ovrimos, PostgreSQL, SQLite, Solid, Sybase, Velocis, Unix dbm
6
C CH HOT NG
9
hello.php
output
11
12
3. Yu cu ci t
Download Apache Server
Download Apache for free here: http://httpd.apache.org/download.cgi
Download PHP
Download PHP for free here: http://www.php.net/downloads.php
13
WAMP Windows
XAMPP Windows/Linux
14
Phn 2: PHP cn bn
Ni dung phn 2
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. PHP v HTML Cu l nh PHP Ghi ch trong PHP Khai bo bi n Ki u d li u Ton t C u trc i u khi n Hm Ph m vi c a bi n Bi n tnh Lm vi c v i file v th m c L p i t ng trong PHP
16
1. PHP v HTML
17
2. Cu lnh PHP
Pht bi u:
Cc cu l nh php cch nhau b i d u ; Khng phn bi t kho ng tr ng, tab hay k t xu ng dng.
V d :
18
V d : <? print cho m i ngu i.; //print dng xu t chu i ?> <?=$bien; <?=$bien; ?> #tng t nh <? print $bien; ?> 19
4. Khai bo bin
C php: $ten_bien = value; Khng khai bo ki u d li u Bi n t ng c kh i t o khi gn gi tr l n u Tn bi n
Bao g m cc k t (A..Z, a..z), k s (0..9),_ Khng c b t u b ng k s (0..9) Phn bi t ch hoa ch th ng
V d :
ng c php: $hoten, $_pass Sai c php: $2host
20
4. Khai bo bin
Variable variables
Cho php thay i tn bi n Vd
<? $varname=Bien_moi; $$varname= xyz; //$Bien_moi= xyz ?>
H ng s - Constants
<?
define("MY_CONST", 10); echo MY_CONST;
?>
21
5. Kiu d liu
boolean integer double string array object M t bi n trong PHP c th lu b t k ki u d li u no
22
5. Kiu d liu
Chuy n i ki u d li u Cch 1 (automatic)
$var = 100 + 15; $var = 100 + 15.0; $var = 39. Steps;//Ch
VD:
23
5. Kiu d liu
Ki m tra ki u d li u gettype is_string is_integer is_array is_double is_object V d
$var = test; if (isset($var)) echo Variable is Set; if (empty($var)) echo Variable is Empty;
24
5. Kiu d liu
M t s hm x l s
Tra H ng D n V d : trong PHP Manual // Pht sinh m t m m ng u nhin $seed = (float) microtime()*100000000; // Kh i t o b pht sinh s ng u nhin srand($seed); // In s ng u nhin print rand(); // Gi tr ng u nhin t 0 n getmaxrand( ) print rand(1, 6); // Gi tr ng u nhin t 1 n 6
25
5. Kiu d liu
Ki u string
Ton t n i chu i .
$str=Hello . World!; //$str = Hello World!;
26
27
28
ph i s
d ng k t
thot: $, \,
Cc hm x
l chu i
Cc x l c b n
strlen($chu strlen($chu i) substr($chu substr($chu i, $v tr, $chi u_di) strtoupper ($chu i) strtolower ($chu i) iconv(m ngu n, m ch, $chu i) trim($chu trim($chu i, k t mu n c t) ltrim($chu ltrim($chu i, k t mu n c t) rtrim($chu rtrim($chu i, k t mu n c t)
32
Cc hm x
Tm ki m
l chu i
strpos($chu strpos($chu i, $chu i_con, $v _tr_b t u) strrpos ($chu i, $chu i_con, $v _tr_b t u)
So snh
strcmp($chu strcmp($chu i_1, $chu i_2) strncmp($chu strncmp($chu i_1, $chu i_2, $chi u_di) strcasecmp($chu strcasecmp($chu i_1, $chu i_2) strncasecmp($chu strncasecmp($chu i_1, $chu i_2, $chi u_di) strnatcmp($chu strnatcmp($chu i_1, $chu i_2) strnatcasecmp($chu strnatcasecmp($chu i_1, $chu i_2)
33
5. Kiu d liu
<?php $n = 43951789; $u = -43951789; $c = 65; // ASCII 65 is 'A' printf("%%b = '%b'\n", $n); printf("%%c = '%c'\n", $c); printf("%%d = '%d'\n", $n); printf("%%e = '%e'\n", $n); printf("%%u = '%u'\n", $n); printf("%%u = '%u'\n", $u); printf("%%f = '%f'\n", $n); printf("%%o = '%o'\n", $n); printf("%%s = '%s'\n", $n); printf("%%x = '%x'\n", $n); printf("%%X = '%X'\n", $n); printf("%%+d = '%+d'\n", $n); printf("%%+d = '%+d'\n", $u); ?>
Hm printf
%b = '10100111101010011010101101' %c = 'A' %d = '43951789' %e = '4.39518e+7' %u = '43951789' %u = '4251015507' %f = '43951789.000000' %o = '247523255' %s = '43951789' %x = '29ea6ad' %X = '29EA6AD' %+d = '+43951789'
%+d = '-43951789' 34
5. Kiu d liu
<?php $input = "Alien"; echo str_pad($input, echo str_pad($input, echo str_pad($input, echo str_pad($input, ?> 10); 10, "-=", STR_PAD_LEFT); 10, "_", STR_PAD_BOTH); 6 , "___"); // // // // produces produces produces produces "Alien " "-=-=-Alien" "__Alien___" "Alien_"
Hm str_pad
STR_PAD_RIGHT : Thm vo bn ph i (m c nh) STR_PAD_LEFT : Thm vo bn tri STR_PAD_BOTH : Thm c hai pha
35
5. Kiu d liu
<?php $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); echo $comma_separated; // lastname,email,phone ?>
<?php // Example 1 $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); echo $pieces[0]; // piece1 echo $pieces[1]; // piece2 // Example 2 $data = "foo:*:1023:1000::/home/foo:/bin/sh"; list($user,$pass,$uid,$gid,$gecos,$home,$shell) = explode(":", $data); echo $user; // foo echo $pass; // * ?>
X l chu i
36
5. Kiu d liu
M ng (Array Array)
<?php $colors = array('red', 'blue', 'green', 'yellow'); sort($colors); foreach ($colors as $color) { echo "Do you like $color?<br>"; Do you like blue? } Do you like green? $num = count($colors); Do you like red? echo We have $num items<br>; Do you like yellow? We have 4 items ?>
37
5. Kiu d liu
M ng (Array Array)
<?php // PHP 5 foreach ($colors as &$color) { $color = strtoupper($color); } unset($color); /* ensure that following writes to $color will not modify the last array element */ // Workaround for older versions (phin b n tr c PHP 5) foreach ($colors as $key => $color) { Array $colors[$key] = strtoupper($color); ( } [0] => RED print_r($colors); ?>
) [1] => BLUE [2] => GREEN [3] => YELLOW
38
5. Kiu d liu
<?php $a = array("a" => "apple", "b" => "banana"); $b = array("a" => "pear", "b" => "strawberry", "c" => "cherry"); $c = $a + $b; // Union of $a and $b echo "Union of \$a and \$b: \n"; var_dump($c); $c = $b + $a; // Union of $b and $a echo "Union of \$b and \$a: \n"; var_dump($c); ?>
Union of $a and $b: array(3) { ["a"]=> string(5) "apple" ["b"]=> string(6) "banana" ["c"]=> string(6) "cherry" }
Union of $b and $a: array(3) { ["a"]=> string(4) "pear" ["b"]=> string(10) "strawberry" ["c"]=> string(6) "cherry" }
39
5. Kiu d liu
<?php /* Suppose that $var_array is an array returned from wddx_deserialize */ $size = "large"; $var_array = array("color" => "blue", "size" => "medium", "shape" => "sphere"); extract($var_array, EXTR_PREFIX_SAME, "wddx"); echo "$color, $size, $shape, $wddx_size\n"; ?>
40
5. Kiu d liu
<?php $info = array('coffee', 'brown', 'caffeine'); // Listing all the variables list($drink, $color, $power) = $info; echo "$drink is $color and $power makes it special.\n"; // Listing some of them list($drink, , $power) = $info; echo "$drink has $power.\n"; // Or let's skip to only the third one list( , , $power) = $info; echo "I need $power!\n"; // list() doesn't work with strings list($bar) = "abcde"; var_dump($bar); // NULL ?> 41
Cc hm x l mng
S px p
Theo gi tr
sort($m sort($m ng) / asort($m ng) asort($m rsort($m rsort($m ng) / arsort($m ng) arsort($m natsort($m natsort($m ng) / natcasesort($m ng) natcasesort($m chu i usort($m usort($m ng, hm_so_snh) // t uasort($m uasort($m ng, hm_so_snh) // t // tng d n // gi m d n // tng d n, dng cho nh ngha th nh ngha th t t
Theo kha
ksort($m ksort($m ng) krsort($m krsort($m ng) uksort($m uksort($m ng, hm_so_snh) // tng d n // gi m d n // t nh ngha th t
42
Cc hm x l mng
N i ghp hai m ng
array_merge($m array_merge($m ng1, $m ng2) array_combine($m array_combine($m ng1, $m ng2) array_intersect($m array_intersect($m ng1, $m ng2)
Tm ki m
array_search($gi_tr array_search($gi_tr , $m ng)
43
6. Ton t
44
6. Ton t
Ex
$a == $b $a === $b $a != $b $a <> $b $a !== $b $a < $b $a > $b $a <= $b $a >= $b Equal Identical Not equal Not equal Not identical Less than Greater than Less than or equal to Greater than or equal to
Name
TRUE if $a is equal to $b.
Result
TRUE if $a is equal to $b, and they are of the same type. (PHP 4) TRUE if $a is not equal to $b. TRUE if $a is not equal to $b. TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4) TRUE if $a is strictly less than $b. TRUE if $a is strictly greater than $b. TRUE if $a is less than or equal to $b. TRUE if $a is greater than or equal to $b.
45
7. Cu trc iu khin
i u ki n if i u khi n switch Vng l p for Tng t Vng l p while Vng l p do.. While Vng l p foreach T kha break, continue
nh C++
46
47
7. Cu trc iu khin
R nhnh v i if else
if ($a > $b) { echo "a > b"; $a = $b; } else { echo "a <= b"; $b = $a; }
48
Cu lnh switch
switch (bi u th c) c) { case bi u th c 1: kh i l nh 1 case bi u th c 2: kh i l nh 2 ... case bi u th c n: kh i l nh n default: default: kh i l nh cu i }
49
7. Cu trc iu khin
R nhnh v i switch case
switch ($a) { case 0: echo "a = 0"; break; case 1: echo "a = 1"; break; }
50
Vng lp while/do...while
while (bi u th c i u ki n) n) kh i l nh Hay: do kh i l nh while (bi u th c i u ki n); n)
51
7. Cu trc iu khin
L p v i while & do while
while ($i++ < 5) { switch ($i) { case 2: echo "At 2"; case 5: echo "At 5"; default: break; } }
break; break 2;
52
Vng lp for
for (bi u th c 1; bi u th c 2; bi u th c 3) 1; kh i l nh
bi u th c 1: th c hi n 1 l n khi b t u vng l p bi u th c 2: i u ki n l p, c xem xt tr c m i l nl p bi u th c 3: th c hi n sau m i l n l p
53
7. Cu trc iu khin
L p v i for & foreach
for ($i=1, $j=0; $i<=10; $j+=$i, print $i, $i++); $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } $arr = array("one", "two", "three"); foreach ($arr as $key => $value) { echo "Key: $key; Value: $value"; }
54
continue
Ngng th c hi n l n l p hi n hnh chuy n sang l n l p ti o theo
55
7. Cu trc iu khin
<?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } // $arr is now array(2, 4, 6, 8) unset($value); // break the reference with the last element ?> <?php $arr = array("one", "two", "three"); reset($arr);// reset pointer, start again on first element while (list(, $value) = each($arr)) { echo "Value: $value<br />\n"; } foreach ($arr as $value) { echo "Value: $value<br />\n"; } ?> 56
7. Cu trc iu khin
<?php $arr = array("one", "two", "three"); reset($arr); while (list($key, $value) = each($arr)) { echo "Key: $key; Value: $value<br />\n"; } foreach ($arr as $key => $value) { echo "Key: $key; Value: $value<br />\n"; } ?> Key: 0; Value: one Key: 1; Value: two Key: 2; Value: three Key: 0; Value: one Key: 1; Value: two Key: 2; Value: three
57
8. Hm
<?php function takes_array($input) { echo "$input[0] + $input[1] = ", $input[0]+$input[1]; } $input = array(4,7); takes_array($input); ?> <?php function add_some_extra(&$string) { $string .= 'and something extra.'; } $str = 'This is a string, '; add_some_extra($str); echo $str;// outputs 'This is a string, and something extra.' ?> 58
lu : d u &
8. Hm
nh ngha
<?php <?php function tn_hm([danh sch tham s ...]) tn_hm( ...]) { [thn hm ...] } ?>
G i hm
Nh p tn hm (khng phn bi t ch in hoa-th ng) v hoacung c p y cc tham s c n thi t trong c p d u ()
59
8. Hm
K t thc v tr k t qu
L nh return dng k t thc v tr k t qu cng nh quy n i u hi n l i cho ni g i hm. N u khng c hm. l nh return th m c nh hm tr v gi tr NULL. NULL. Mu n tr v hn m t gi tr th ph i dng m ng
Truy n tham s
M c nh cc tham s c truy n vo bn trong hm theo phng php tham tr . Tr ng h p mu n thay i tr c ti p trn cc tham s truy n th ng i ta dng phng php tham chi u, thm d u & tr c tn tham s (khi nh ngha) cng nh tn bi n c truy n lm tham (khi g i hm)
60
8. Hm
Tham s c gi tr m c nh
Tng t cch khai bo v gn gi tr u tin cho bi n, thng th ng lo i tham s ny nn t cu i trong danh sch tham s Khi g i hm n u b tr ng t i v tr tham s c gi tr m c nh th m c nhin gi tr m c nh c dng cho tham s
Hm c s l ng tham s khng xc nh
Khai bo danh sch tham s r ng () S d ng cc hm sau l y danh sch cc tham s : func_num_args(): s l ng tham s khi hm c g i func_get_arg(i): gi tr cc tham s th i c truy n (b t u t 0) func_get_args(): danh sch t t c cc tham s
61
8. Hm
Bi n tnh
Thm t kha static khi khai bo bi n c kh i t o (v gn gi tr ) m t l n u tin duy nh t trong su t qu trnh th c thi c a script
S d ng bi n ton c c
Khai bo l i bi n ton c c v i t kha global (bn trong hm) c th s d ng c bi n ton c c ny bn trong hm S d ng cc hm sau l y danh sch cc tham s : func_num_args(): func_num_args(): s l ng tham s khi hm c g i func_get_arg(i): func_get_arg(i): gi tr cc tham s th i c truy n (b t u t 0) func_get_args(): danh sch t t c cc tham s
62
8. Hm
Ph m vi
C gi tr s d ng trong ton script, ngay c tr c v sau khi nh ngha
L ng hm
Cho php nh ngha l ng hm, th m ch l ng bn trong m t c u trc i u khi n (if, switch, while/do, while) Lo i hm ny c ph m vi trong ton script v khng th nh ngha l i
63
8. Hm
<?php function makecoffee($type = "cappuccino") { return "Making a cup of $type.\n"; } echo makecoffee(); echo makecoffee(null); echo makecoffee("espresso"); ?>
<?php function makecoffee($types = array("cappuccino"), $coffeeMaker = NULL) { $device = is_null($coffeeMaker) ? "hands" : $coffeeMaker; return "Making a cup of ".join(", ", $types)." with $device.\n"; } echo makecoffee(); echo makecoffee(array("cappuccino", "lavazza"), "teapot"); ?>
64
8. Hm
<?php function makeyogurt($type = "acidophilus", $flavour) { return "Making a bowl of $type $flavour.\n"; } echo makeyogurt("raspberry");// won't work as expected ?>
<?php function makeyogurt($flavour, $type = "acidophilus") { return "Making a bowl of $type $flavour.\n"; } echo makeyogurt("raspberry"); ?> // works as expected
65
8. Hm
<?php function square ($num) { return $num * $num; } echo square (4);//outputs '16 ?> <?php function &returns_reference() { return $someref; } $newref=&returns_reference(); ?>
66
8. Hm
<?php function foo() { echo "In foo()<br />\n"; } function bar($arg = '') { echo "In bar(); argument was '$arg'.<br />\n"; } // This is a wrapper function around echo function echoit($string) { echo $string; } $func = 'foo'; $func(); // This calls foo() $func = 'bar'; $func('test');
67
8. Hm
<?php class Foo { function Variable() { $name = 'Bar'; $this->$name(); // This calls the Bar() method } function Bar() { echo "This is Bar"; } } $foo = new Foo(); $funcname = "Variable"; $foo->$funcname(); // This calls $foo->Variable() ?> 68
9. Phm vi ca bin
<?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?> <?php $a = 1; $b = 2; function Sum() { $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"]; } Sum(); echo $b; ?>
69
9. Phm vi ca bin
<?php $a = 1; include "b.inc"; ?> Ph m vi c a bi n [a] c tc d ng trong c b.inc ---------------------------------------------<?php $a = 1; // global scope function Test() { echo $a;// reference to local scope variable } Test(); ?> Bi n [a] ngoi v trong hm Test khc nhau
70
9. Phm vi ca bin
Tn bi n $GLOBALS $_SERVER $_GET $_POST $_COOKIE $_FILE $_ENV M t
ch a t t c cc bi n ton c c m o n script c c b c th truy c p ch a thng tin v mi tr ng c a web server ch a thng tin c a yu c u d ng GET ch a thng tin c a yu c u d ng POST ch a thng tin t HTTP cookie ch a thng tin t vi c t i t p tin ln ki u POST ch a thng tin v mi tr ng th c thi c a o n script
?
72
73
dng l nh ini_set()
<?php var_dump(ini_get("include_path")); ini_set("include_path", "/inc"); // Works in all PHP versions var_dump(ini_get("include_path")); ini_restore("include_path"); var_dump(ini_get("include_path")); ?> 79
81
echo "Read line by line......\n"; $f = fopen("test.txt", "rt"); echo "Read all file......\n"; $myfile = while (!feof($f)) { file_get_contents("test.txt"); $line = fgets($f); echo($myfile); echo "$line"; } ?> fclose($f);
82
85
Bi tp
Vi t m t script th c hi n cng vi c sau:
Yu c u ng i dng nh p vo m t s nguyn em so snh s v a nh p v i m t s nguyn cho tr c N u b ng th xu t ra cu chc m ng ng i dng on ng con s b m t Ng c l i th th thng bo k t qu l con s v a nh p l l n hay b hn con s b m t v yu c u nh p l i cho n khi nh p ng
86
Hm t o
88
T o v h y m t i t ng
$tn_bi n = new tn_l p(); i t ng s t ng b h y khi khng cn tham chi u no n n $tn_bi n = NULL; NULL;
89
M t s quy t c chung:
Khng th khai bo hai method trng tn Method ph i c khai bo ngay bn trong khai bo l p Dng bi n gi $this truy xu t cc member v method trong l p Dng ton t -> truy xu t n member v method
90
91
92
Truy xu t
tn_l p::TN_H NG p::TN_H seft::TN_H seft::TN_H NG // ngoi l p // trong l p
93
Truy xu t
tn_l p::$thu c_tnh // ngoi l p seft::$thu c_tnh // trong l p
94
Truy xu t
tn_l p::phng_th c() // ngoi l p seft:: phng_th c() // trong l p
95
96
L p trnh h ng i t ng:
phng th c n p ch ng G i m t method l p cha
parent::phng_th parent::phng_th c()
B ng cch nh ngha l i m t phng th c c l p cha, t t c cc l i g i n phng th c ny m khng ch nh r nh trn s c hi u l g i phng th c c cng tn c a l p con
97
a hnh
Phng th c khng th n p ch ng
final public function khng_th _n p_ch ng() { }
99
101
Cu hi v tho lun
103