Bài Giảng Lập Trình Web -Chương 6: Lập trình Web với PHP pdf

20 380 0
Bài Giảng Lập Trình Web -Chương 6: Lập trình Web với PHP pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Trình bày: NguyễnPhúTrường NộIDUNG  Giớithiệuvề PHP  Biến, kiểudữ liệu, phép toán  Lệnh điềukhiển  Hàm  PHP kếthợpvới forms  Cookies, SSI (Server side includes), Date  PHP-MySQL 1/5/2011 2 Bộ môn Mạng máy tính & Truyền thông  Giớithiệuvề PHP  Biến, kiểudữ liệu, phép toán  Lệnh điềukhiển  Hàm  PHP kếthợpvới forms  Cookies, SSI (Server side includes), Date  PHP-MySQL 1/5/2011 3 Bộ môn Mạng máy tính & Truyền thông GIớITHIệUVề PHP n PHP là gì ? l PHP là Hypertext Preprocessor l Ngôn ngữ script chạytrênserver l PHP scripts chứa text, thẻ HTML, script l Sử dụng phầnmở rộng tên file : .php, .phtml l PHP scripts sẽ trả về kếtquả cho trình duyệtmột plain HTML l PHP hỗ trợđểlàm việcvới nhiềuhệ QTCSDL khác nhau: MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, l Phầnmềmmãnguồnmở, miễn phí l Chạytrênnhiều platforms (Unix, Linux, Windows) 1/5/2011 4 Bộ môn Mạng máy tính & Truyền thông GIớITHIệUVề PHP n MySQL là gì ? l Hệ quảntrị cơ sở dữ liệu l Dùng cho các ứng dụng vừavànhỏ l Hỗ trợ chuẩnSQL l Phầnmềmmãnguồnmở, miễn phí l Chạytrênnhiều platforms (Unix, Linux, Windows) l Phổ biến l PHP + MySQL : Web động chạytrênnhiều platforms khác nhau 1/5/2011 5 Bộ môn Mạng máy tính & Truyền thông GIớITHIệUVề PHP n Tại sao PHP ? l Chạytrênnhiều platforms khác nhau (Unix, Linux, Windows) l Phầnmềmmãnguồnmở, miễn phí l Tương thích vớihầuhết các web server (Apache, IIS, etc) l Dễ họcvàpháttriển nhanh các ứng dụng trên Web n Làm thế nào để sử dụng PHP l Cài web server (Apache, IIS, etc) l Cài MySQL l Cài PHP l Địachỉ : www.apache.org, www.php.net, www.mysql.com l Cài Vertrigo hay Xamp, Chứacả Apache, MySQL và PHP 1/5/2011 6 Bộ môn Mạng máy tính & Truyền thông  Giớithiệuvề PHP  Biến, kiểudữ liệu, phép toán  Lệnh điềukhiển  Hàm  PHP kếthợpvới forms  Cookies, SSI (Server side includes), Date  PHP-MySQL 1/5/2011 7 Bộ môn Mạng máy tính & Truyền thông 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> 1/5/2011 8 Bộ môn Mạng máy tính & Truyền thông CÚ PHÁP PHP  Cú pháp  Khốilệnh PHP script bắt đầuvới <?php và kết thúc bởi?>  Khốilệnh có thểđược đặtbấtcứ nơi nào trong tài liệu  Mỗilệnh cách nhau bởidấ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ănbản*/ Bộ môn Mạng máy tính & Truyền thông 91/5/2011 CÚ PHÁP PHP Cú pháp l Ví dụ : <?php 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"); ?> 1/5/2011 10 Bộ môn Mạng máy tính & Truyền thông BIếN n Biến trong PHP l Chứadữ liệu l Biến đượcbắt đầubởidấu $ l Tên biếnbắt đầubằng mộtkýtự chữ cái hoặc _ l Phân biệtgiữakýtự thường và hoa l Kiểu đượctínhở thời điểm gán giá trị l Gán giá trị với = l Sử dụng & như tham chiếu 1/5/2011 11 Bộ môn Mạng máy tính & Truyền thông BIếN n Biến trong PHP l Ví dụ : <?php $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. ?> 1/5/2011 12 Bộ môn Mạng máy tính & Truyền thông BIếN n Biến trong PHP l Ví dụ : <?php $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 ?> 1/5/2011 13 Bộ môn Mạng máy tính & Truyền thông BIếN n Biến trong PHP l Ví dụ : <?php $foo = 'Bob'; echo $foo; // Bob $foo = 12 echo $foo; // 12 $foo = array(1, 2, 3, 4, 5); for($i = 0; $i < 5; $i++) echo $bar[$i] . "<br>"; ?> 1/5/2011 14 Bộ môn Mạng máy tính & Truyền thông BIếN n BiếncósẵntrongPHP l $GLOBALS : tấtcả các biến trong phạm vi toàn cụccủascript l $_SERVER : tậphợpbiếnmôitrường của Web server l $_GET, $_POST : biến được cung cấp các chuỗi query URL cho script l $_COOKIE : biến cung cấp HTTP_cookies cho script l $_FILES : biếncungcấp HTTP POST file uploads cho script l $_ENV : biếncungcấpmôitrường cho script l $_REQUEST : cung cấp các $_GET, $_POST, $_COOKIE 1/5/2011 15 Bộ môn Mạng máy tính & Truyền thông BIếN n Phạmvi biến l Toàn cục: sử dụng từ khóa global hoặcbiến $GLOBALS l Ví dụ : <?php $a = 1; include 'b.inc'; // biến$a sẵn dùng trong b.inc ?> 1/5/2011 16 Bộ môn Mạng máy tính & Truyền thông BIếN n Phạmvi biến l Toàn cục: sử dụng từ khóa global hoặcbiến $GLOBALS l Ví dụ : <?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?> 1/5/2011 17 Bộ môn Mạng máy tính & Truyền thông BIếN n Phạmvi biến l Toàn cục: sử dụng từ khóa global hoặcbiến $GLOBALS l Ví dụ : <?php $a = 1; $b = 2; function Sum() { $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b']; } Sum(); echo $b; ?> 1/5/2011 18 Bộ môn Mạng máy tính & Truyền thông BIếN n Phạmvi biến l Cụcbộ l Ví dụ : <?php $a = 1; /* global scope */ function Test() { $a = 10; echo “ in Test a = “ . $a; /* reference to local scope variable */ } Test(); echo “<br> out Test a = “ . $a; ?> 1/5/2011 19 Bộ môn Mạng máy tính & Truyền thông BIếN 20 n Phạmvi biến l Biếntĩnh : sử dụng từ khóa static l Ví dụ : <?php function Test() { static $a = 10; echo “ in Test a = “ . $a; $a++; } Test(); // 10 Test(); // 11 ?> 1/5/2011 20 Bộ môn Mạng máy tính & Truyền thông KIểU 21 n Kiểudữ liệucơ bản l Số nguyên : 4 bytes, số có dấu l Số thực l Luận lý : TRUE/FALSE l Chuỗikýtự n Kiểudữ liệuphứchợp l mảng l Đốitượng l Kiểugiả l Etc. 1/5/2011 21 Bộ môn Mạng máy tính & Truyền thông KIểU 22 n Kiểudữ liệu l Ví dụ : số nguyên, số thực <?php $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; ?> 1/5/2011 22 Bộ môn Mạng máy tính & Truyền thông KIểU 23 n Kiểudữ liệu l Ví dụ : luậnlý <?php $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"; } ?> 1/5/2011 23 Bộ môn Mạng máy tính & Truyền thông KIểU 24 n Kiểudữ liệu l Ví dụ : chuỗi <?php $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 ?> 1/5/2011 24 Bộ môn Mạng máy tính & Truyền thông KIểU 25 n Kiểudữ liệu l mảng array( [key =>] value , ) // key may be an integer or string // value may be any value l Ví dụ : <?php $arr = array("foo" => "bar", 12 => 1); echo $arr["foo"]; // bar echo $arr[12]; // 1 ?> 1/5/2011 25 Bộ môn Mạng máy tính & Truyền thông KIểU 26 n Kiểudữ liệu l mảng, ví dụ : <?php $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); ?> 1/5/2011 26 Bộ môn Mạng máy tính & Truyền thông KIểU 27 n Kiểudữ liệu l Truy xuất các phầntử mảng: $array_name[key] l Ví dụ : <?php $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 ?> 1/5/2011 27 Bộ môn Mạng máy tính & Truyền thông KIểU 28 n Kiểu dữ liệu l Ví dụ : mảng <?php $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] . “<br>”; ?> 1/5/2011 28 Bộ môn Mạng máy tính & Truyền thông PHÉP TOÁN 29 1/5/2011 29 Bộ môn Mạng máy tính & Truyền thông Phép toán 30 1/5/2011 30 Bộ môn Mạng máy tính & Truyền thông PHÉP TOÁN 31 1/5/2011 31 Bộ môn Mạng máy tính & Truyền thông PHÉP TOÁN 32 1/5/2011 32 Bộ môn Mạng máy tính & Truyền thông  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 1/5/2011 33 Bộ môn Mạng máy tính & Truyền thông ĐIềU KIệN 34 n IF l Cú pháp : if (condition) code to be executed if condition is true; else code to be executed if condition is false; l Ví dụ : <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?> 1/5/2011 34 Bộ môn Mạng máy tính & Truyền thông ĐIềU KIệN 35 n Switch l 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; } 1/5/2011 35 Bộ môn Mạng máy tính & Truyền thông ĐIềU KIệN 36 n Switch l Ví dụ : <?php 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"; } ?> 1/5/2011 36 Bộ môn Mạng máy tính & Truyền thông LặP 37 n While l Cú pháp : while (condition) code to be executed; l Ví dụ : <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br />"; $i++; } ?> 1/5/2011 37 Bộ môn Mạng máy tính & Truyền thông LặP 38 n Do … while l Cú pháp : do { code to be executed; } while (condition); l Ví dụ : <?php $i=0; do { $i++; echo "The number is " . $i . "<br />"; } while ($i<5); ?> 1/5/2011 38 Bộ môn Mạng máy tính & Truyền thông LặP 39 n For l Cú pháp : for (initialization; condition; increment) { code to be executed; } l Ví dụ : <?php for ($i=1; $i<=5; $i++) { echo "Hello World!<br />"; } ?> 1/5/2011 39 Bộ môn Mạng máy tính & Truyền thông LặP 40 n Foreach l Cú pháp : foreach (array as value) { code to be executed; } l Ví dụ : <?php $arr=array("one", "two", "three"); foreach ($arr as $value) { echo "Value: " . $value . "<br />"; } ?> 1/5/2011 40 Bộ môn Mạng máy tính & Truyền thông [...]... Lệnh điều khiển Hàm PHP kết hợp với forms Cookies, SSI (Server side includes), Date PHP- MySQL Ví dụ : < ?php function &returns_reference() { return $someref; } $newref =& returns_reference(); ?> 1/5/2011 51 Bộ môn Mạng máy tính & Truyền thông 51 1/5/2011 52 PHP + HTML FORM PHP + HTML FORM n PHP kết hợp với HTML Form n Hầu hết các thành phần của HTML Form đều được sẵn dùng trong các PHP script Sử dụng... sử dụng method="POST" PHP script welcome .php nội dung như sau Welcome < ?php echo $_POST["name"]; ?>. You are < ?php echo $_POST["age"]; ?> years old! 53 1/5/2011 54 Bộ môn Mạng máy tính & Truyền thông 54 COOKIES n 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 1/5/2011 55... mới, các đặc trưng mới được hỗ trợ, các bản tin về PHP Download PHP Download các thư viện lập trình ứng dụng Download được các công cụ hỗ trợ, các trình tiện ích Tài liệu về PHP 1/5/2011 74 75 1/5/2011 76 76 Bộ môn Mạng máy tính & Truyền thông TÀI LIệU THAM KHảO Sách: [Luke Welling & Laura Thomson, 2001] Luke Welling & Laura Thomson, PHP and MySQL Web Development”, SAMS, 2001 [Rasmus Lerdorf & Kevin...ĐịNH NGHĨA SẵN TRONG PHP 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 1/5/2011 41 Bộ môn Mạng máy tính & Truyền thông 41 1/5/2011 HÀM DO NGƯờI Sử DụNG ĐịNH NGHĨA n 42 Bộ môn Mạng máy tính & Truyền thông 42 HÀM DO NGƯờI Sử DụNG ĐịNH NGHĨA Hàm l Cú pháp : < ?php function foo($arg_1, $arg_2, /*... Sklar & Adam Trachtenberg, PHP Cookbook”, O'Reilly, 2002 [Simon Stobart & Mike Vassileiou, 2004] Simon Stobart & Mike Vassileiou, Php and Mysql Manual: Simple, Yet Powerful Web Programming”, Springer Professional Computing, 2004 [Hugh E Williams & David Lane, 2002] Hugh E Williams & David Lane, Web Database Applications with PHP & MySQL”, O'Reilly, 2002 Cung cấp: Thông tin về PHP như: phiên bản mới,... HTML Form Ví dụ : trang web là welcome.html nội dung như sau l l l Enter your name: Enter your age: 1/5/2011 53 Bộ môn Mạng máy tính & Truyền thông PHP kết hợp với HTML Form l l PHP script "welcome .php" sử dụng biến $_POST... code chương trình của một file vào file khác trước khi thực thi Sử dụng hàm require() Ví dụ : Cú pháp : string date (date_format[,int timestamp]) 59 Bộ môn Mạng máy tính & Truyền thông 59 1/5/2011 60 Bộ môn Mạng máy tính & Truyền thông 60 HÀM THờI GIAN n Date() 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... + + 5 rows in set (0.00 sec) mysql> 1/5/2011 69 PHP nối kết đến MySQL Bộ môn Mạng máy tính & Truyền thông 69 1/5/2011 PHP NốI KếT ĐếN MYSQL n 70 Bộ môn Mạng máy tính & Truyền thông 70 PHP NốI KếT ĐếN MYSQL PHP nối kết đến MySQL Giải phóng tài nguyên của kết quả mysql_free_result($result); l Đóng kết nối mysql_close($conn); l < ?php $conn = mysql_connect("127.0.0.1", "nghi", "nghi")... < ?php function square($num) { return $num * $num; } echo square(4); // outputs '16' ?> 1/5/2011 < ?php function small_numbers() { return array (0, 1, 2); } list ($zero, $one, $two) = small_numbers(); ?> 49 Bộ môn Mạng máy tính & Truyền thông 49 1/5/2011 50 Bộ môn Mạng máy tính & Truyền thông 50 Bộ môn Mạng máy tính & Truyền thông 52 HÀM DO NGƯờI Sử DụNG ĐịNH NGHĨA n Giá trị trả về l Giới thiệu về PHP. .. l l l l Bộ môn Mạng máy tính & Truyền thông 55 Thường được sử dụng để xác định một user Server ghi 1 tập tin cookie lên web client PHP cho phép tạo và đọc lại những giá trị từ cookie Hàm tạo cookie : setcookie(name, value, expire, path, domain) Được đặt trước thẻ Ví dụ : < ?php setcookie("uname", $name, time()+36000); ?> A cookie was set on this page! The cookie will be active . thông GIớITHIệUVề PHP n PHP là gì ? l PHP là Hypertext Preprocessor l Ngôn ngữ script chạytrênserver l PHP scripts chứa text, thẻ HTML, script l Sử dụng phầnmở rộng tên file : .php, .phtml l PHP scripts. Tương thích vớihầuhết các web server (Apache, IIS, etc) l Dễ họcvàpháttriển nhanh các ứng dụng trên Web n Làm thế nào để sử dụng PHP l Cài web server (Apache, IIS, etc) l Cài MySQL l Cài PHP l Địachỉ. value="welcome"> </form> </body> </html> 1/5/2011 53 Bộ môn Mạng máy tính & Truyền thông PHP + HTML FORM PHP + HTML FORM 54 n PHP kết hợp với HTML Form l PHP script "welcome .php& quot; sử dụng biến $_POST để truy xuất đến các

Ngày đăng: 28/06/2014, 15:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan