Khoa Công Ngh Thông Tin
Tr ng i H c C n Thơ
L p Trình PHP
Thanh Ngh
C n Th
24-11-2005
N i dung
Gi i thi u v PHP
Bi n, ki u d li u, phép toán
L nh i u khi n
Hàm
PHP k t h p v i forms
Cookies, SSI (Server side includes), Date
PHP-MySQL
Printed with FinePrint trial version - purchase at www.fineprint.com
2
Gi i thi u v PHP
Bi n, ki u d li u, phép toán
L nh i u khi n
Hàm
PHP k t h p v i forms
Cookies, SSI (Server side includes), Date
PHP-MySQL
3
Gi i thi u v PHP
PHP là gì ?
PHP là Hypertext Preprocessor
Ngôn ng script ch y trên server
PHP scripts ch a text, th HTML, script
S d ng ph n m r ng tên file : .php, .phtml
PHP scripts s tr v k t qu cho trình duy t m t plain HTML
PHP h tr
làm vi c v i nhi u h QTCSDL khác nhau
MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL,
Generic ODBC, etc.
Ph n m m mã ngu n m , mi n phí
Ch y trên nhi u platforms (Unix, Linux, Windows)
Printed with FinePrint trial version - purchase at www.fineprint.com
4
Gi i thi u v PHP
MySQL là gì ?
H qu n tr c s d li u
Dùng cho các ng d ng v a và nh
H tr chu n SQL
Ph n m m mã ngu n m , mi n phí
Ch y trên nhi u platforms (Unix, Linux, Windows)
Ph bi n
PHP + MySQL : Web ng ch y trên nhi u platforms khác nhau
5
Gi i thi u v PHP
T i sao PHP ?
Ch y trên nhi u platforms khác nhau (Unix, Linux, Windows)
Ph n m m mã ngu n m , mi n phí
Tư ng thích v i h u h t các web server (Apache, IIS, etc)
D h c và phát tri n nhanh các ng d ng trên Web
Làm th nào
s d ng PHP
Cài web server (Apache, IIS, etc)
Cài MySQL
Cài PHP
a ch : www.apache.org, www.php.net, www.mysql.com
Printed with FinePrint trial version - purchase at www.fineprint.com
6
Gi i thi u v PHP
Bi n, ki u d li u, phép toán
L nh i u khi n
Hàm
PHP k t h p v i forms
Cookies, SSI (Server side includes), Date
PHP-MySQL
7
Cú pháp PHP
Cú pháp
PHP scripts ch a text, th HTML, script
Ví d : in ra màn hình chu i “Hello World”
<html>
<body>
<?php echo "Hello World"; ?>
</body>
</html>
Printed with FinePrint trial version - purchase at www.fineprint.com
8
Cú pháp PHP
Cú pháp
Kh i l nh PHP script b!t u v i <?php và k t thúc b i ?>
Kh i l nh có th ư c "t b#t c n i nào trong tài li u
M i l nh cách nhau b i d#u ;
Có 2 l nh c b n in text ra màn hình : echo và print
Chú thích trong chư ng trình
// chú thích là 1 dịng n
/* chú thích là 1 o n
v$n b n */
9
Cú pháp PHP
Cú pháp
Ví d :
echo "This is a test"; // This is a one-line c++ style comment
/* This is a multi line comment
yet another line of comment */
echo("This is yet another test");
print "Hello World";
print("Hello World");
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
10
Bi n
Bi n trong PHP
Ch a d li u
Bi n ư c b!t u b i d#u $
Tên bi n b!t u b%ng m t ký t& ch cái ho"c _
Phân bi t gi a ký t& thư'ng và hoa
Ki u ư c tính th'i i m gán giá tr
Gán giá tr v i =
S d ng & như tham chi u
11
Bi n
Bi n trong PHP
Ví d :
$var = 'Bob';
$Var = 'Joe';
echo "$var, $Var"; // outputs "Bob, Joe"
$4site = 'not yet'; // invalid; starts with a number
$_4site = 'not yet'; // valid; starts with an underscore
$täyte = 'mansikka'; // valid; 'ä' is (Extended) ASCII 228.
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
12
Bi n
Bi n trong PHP
Ví d :
$foo = 'Bob';
// Assign the value 'Bob' to $foo
$bar = &$foo;
// Reference $foo via $bar.
$bar = "My name is $bar"; // Alter $bar...
echo $bar;
// My name is Bob
echo $foo;
// My name is Bob
?>
13
Bi n
Bi n trong PHP
Ví d :
$foo = 'Bob';
echo $foo;
// Bob
$foo = 12
echo $foo;
// 12
$foo = array(1, 2, 3, 4, 5);
for($i = 0; $i < 5; $i++)
echo $foo[$i] . "
";
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
14
Bi n
Bi n có s(n trong PHP
$GLOBALS : t#t c các bi n trong ph m vi toàn c c c)a script
$_SERVER : t*p h p bi n môi trư'ng c)a Web server
$_GET, $_POST : bi n ư c cung c#p các chu i query URL
cho script
$_COOKIE : bi n cung c#p HTTP_cookies cho script
$_FILES : bi n cung c#p HTTP POST file uploads cho script
$_ENV : bi n cung c#p môi trư'ng cho script
$_REQUEST : cung c#p các $_GET, $_POST, $_COOKIE
15
Bi n
Ph m vi bi n
Toàn c c : s d ng t khóa global ho"c bi n $GLOBALS
Ví d :
$a = 1;
include 'b.inc'; // bi n $a s(n dùng trong b.inc
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
16
Bi n
Ph m vi bi n
Toàn c c : s d ng t khóa global ho"c bi n $GLOBALS
Ví d :
$a = 1;
$b = 2;
function Sum() {
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
17
Bi n
Ph m vi bi n
Toàn c c : s d ng t khóa global ho"c bi n $GLOBALS
Ví d :
$a = 1;
$b = 2;
function Sum() {
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Sum();
echo $b;
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
18
Bi n
Ph m vi bi n
C cb
Ví d :
$a = 1; /* global scope */
function Test() {
$a = 10;
echo “ in Test a = “ . $a; /* reference to local scope variable */
}
Test();
echo “
out Test a = “ . $a;
?>
19
Bi n
Ph m vi bi n
Bi n t+nh : s d ng t khóa static
Ví d :
function Test() {
static $a = 10;
echo “ in Test a = “ . $a;
$a++;
}
Test(); // 10
Test(); // 11
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
20
Ki u
Ki u d li u c b n
S nguyên : 4 bytes, s có d#u
S th&c
Lu*n lý : TRUE/FALSE
Chu i ký t&
Ki u d li u ph c h p
m ng
i tư ng
Ki u gi
Etc.
21
Ki u
Ki u d li u
Ví d : s nguyên, s th&c
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$b = 1.234;
$c = 1.2e3;
$d = 7E-10;
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
22
Ki u
Ki u d li u
Ví d : lu*n lý
$foo = True; // assign the value TRUE to $foo
if ($action == "show_version") {
echo "The version is 1.23";
}
// this is not necessary...
if ($show_separators == TRUE) {
echo "<hr>\n";
}
// ...because you can simply type
if ($show_separators) {
echo "<hr>\n";
} ?>
23
Ki u
Ki u d li u
Ví d : chu i
$beer = 'Heineken';
echo "$beer's taste is great"; // works, "'" is an invalid character for varnames
echo "He drank some $beers"; // won't work, 's' is a valid character for varnames
echo "He drank some ${beer}s"; // works
echo "He drank some {$beer}s"; // works
$str = 'This is a test.';
$third = $str{2}; // Get the third character of a string
$str = "This is still a test.";
$last = $str{strlen($str)-1}; // Get the last character of a string.
$str = 'Look at the sea';
$str{strlen($str)-1} = 'e'; // Modify the last character of a string
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
24
Ki u
Ki u d li u
m ng
array( [key =>] value
, ...
)
// key may be an integer or string
// value may be any value
Ví d :
$arr = array("foo" => "bar", 12 => 1);
echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
25
Ki u
Ki u d li u
m ng, ví d :
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
echo $arr["somearray"][6]; // 5
echo $arr["somearray"][13]; // 9
echo $arr["somearray"]["a"]; // 42
// This array is the same as ...
$a = array(5 => 43, 32, 56, "b" => 12);
// ...this array
$a_n = array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
26
Ki u
Ki u d li u
Truy xu#t các ph n t m ng: $array_name[key]
Ví d :
$arr = array(5 => 1, 12 => 2);
$arr[] = 56; // This is the same as $arr[13] = 56;
$arr["x"] = 42; // This adds a new element to the array with key "x"
unset($arr[5]); // This removes the element from the array
unset($arr); // This deletes the whole array
?>
27
Ki u
Ki u d li u
Ví d : m ng
$array = array(1, 2, 3, 4, 5); // Create a simple array.
print_r($array);
foreach ($array as $i => $value) // Now delete every item, but leave the array itself intact:
echo $array[$i] . “
”;
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
28
Phép toán
29
Phép toán
Printed with FinePrint trial version - purchase at www.fineprint.com
30
Phép toán
31
Phép toán
Printed with FinePrint trial version - purchase at www.fineprint.com
32
Gi i thi u v PHP
Bi n, ki u d li u, phép toán
L nh i u khi n
Hàm
PHP k t h p v i forms
Cookies, SSI (Server side includes), Date
PHP-MySQL
33
i u ki n
IF
Cú pháp :
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Ví d :
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
34
i u ki n
Switch
Cú pháp :
switch (expression) {
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}
35
i u ki n
Switch
Ví d :
switch ($x) {
case 1:
echo "Number 1"; break;
case 2:
echo "Number 2"; break;
case 3:
echo "Number 3"; break;
default:
echo "No number between 1 and 3";
}
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
36
L"p
While
Cú pháp :
while (condition)
code to be executed;
Ví d :
$i=1;
while($i<=5) {
echo "The number is " . $i . "
";
$i++;
}
?>
37
L"p
Do … while
Cú pháp :
do {
code to be executed;
} while (condition);
Ví d :
$i=0;
do {
$i++;
echo "The number is " . $i . "
";
} while ($i<5);
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
38
L"p
For
Cú pháp :
for (initialization; condition; increment) {
code to be executed;
}
Ví d :
for ($i=1; $i<=5; $i++)
{
echo "Hello World!
";
}
?>
39
L"p
Foreach
Cú pháp :
foreach (array as value) {
code to be executed;
}
Ví d :
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "
";
}
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
40
Gi i thi u v PHP
Bi n, ki u d li u, phép toán
L nh i u khi n
Hàm
PHP k t h p v i forms
Cookies, SSI (Server side includes), Date
PHP-MySQL
Printed with FinePrint trial version - purchase at www.fineprint.com
41
Hàm nh
ngh+a s(n
trong PHP
42
Hàm do ngư'i s d ng
nh ngh+a
Hàm
Cú pháp :
function foo($arg_1, $arg_2, /* ..., */ $arg_n)
{
echo "Example function.\n";
return $retval;
}
?>
43
Hàm do ngư'i s d ng
nh ngh+a
Printed with FinePrint trial version - purchase at www.fineprint.com
44
Hàm do ngư'i s d ng
nh ngh+a
45
Hàm do ngư'i s d ng
nh ngh+a
Tham s
Truy n tham s : giá tr , tham chi u
Hàm : func_num_args(), func_get_arg()
Ví d tham s là m ng:
function takes_array($input) {
echo "$input[0] + $input[1] = ", $input[0]+$input[1];
}
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
46
Hàm do ngư'i s d ng
nh ngh+a
Tham s
Ví d tham s có giá tr m"c
nh :
function makecoffee($type = "cappuccino")
{
return "Making a cup of $type.
";
}
echo makecoffee();
echo makecoffee("espresso");
?>
47
Hàm do ngư'i s d ng
nh ngh+a
Tham s
Ví d truy n tham chi u :
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.'
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
48
Hàm do ngư'i s d ng
nh ngh+a
Giá tr tr v
Ví d :
function square($num)
{
return $num * $num;
}
echo square(4); // outputs '16'.
?>
49
Hàm do ngư'i s d ng
nh ngh+a
Giá tr tr v
Ví d :
function small_numbers()
{
return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
?>
Printed with FinePrint trial version - purchase at www.fineprint.com
50