PHP. Bài 1 Giới thiệu. Giới thiệu Cài đặt và cấu hình PHP Các khái niệm cơ bản trong PHP. Giới thiệu. Các ngôn ngữ sử dụng trong Web Ngôn ngữ định dạng: html, xml Ngôn ngữ lập trình: Phía client: Javascript, VBScript Phía server: PHP, ASP, JSP, …. Giới thiệu PHP.
An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
Giớithiệu • Các ngôn ngữ sử dụng trong Web • Ngôn ngữ định dạng: html, xml • Ngôn ngữ lập trình: • Phía client: Javascript, VBScript • Phía server: PHP, ASP, JSP, …
Giới thiệu PHP • PHP viết tắt của Personal Home Page • Là 1 server script dùng để xây dựng các ứng dụng web • Ra đời năm 1994 • Là phát minh của Rasmus Lerdorf
Cài đặt PHP • Có thể cài đặt riêng lẽ PHP bằng cách download gói PHP từ http://php.net/downloads.php và sau đó cài đặt, cấu hình Web server (IIS hay Apache). • Đễ dễ dàng hơn sử dụng gói cài đặt XAMPP http://www.apachefriends.org/en/index.html (chọn xampp Installer version mới nhất là 1.7.4 tên file là xampp-win32-1.7.4-VC6-installer.exe)
Cài đặt XAMPP • Tắt dịch vụ IIS (nếu máy có cài IIS) • Cài xampp vào 1 thư mục (VD: C:\xamp) • Vận hành Apache, PHP, MySQL (chạy C:\xampp\ ampp-control.exe) • Start Apache và MySQL • Test: http://localhost
Test PHP • Mở notepad nhập <?php phpinfo(); ?> • Lưu lại với tên là test.php trong thư mục cài đặt xamp\htdocs. Ví dụ: C:\xampp\htdocs\test.php • http://localhost/test.php
PHP căn bản (tt) • Xuất kết quả ra trang web • echo (string) hay echo string. Với string là 1 chuỗi Ví dụ <?php echo “Xin chào” ?> • Khai báo biến • $ten_bien [= gia_tri_khoi_tao];
Object <?php class C { public/protected/private $variable; function C() //PHP 4 { echo "Đây làphươngthứckhởitạocủalớp C"; } function __construct(){} //PHP5 function F() { echo $this->variable; } function __destruct()//PHP 5 } $c=new C; //tạo 1 đốitượnglớp C $c->F(); // gọiphươngthức F củalớp C ?>
Hằngsố • Địnhnghĩa hằng define("pi",3.14); • Gọi hằng echo "pi = ".pi; • Kiểmtra hằng Để kiểmtrahằngsố x cótồntạichưathìdùnghàm define("x"). Với x là tên hằng số Ví dụ: define("pi");
Phátbiểucóđiềukiện • Cúpháp if (điềukiện) { //khối lệnh 1} else {//khối lệnh 2} • Cácphátbiểu if cóthể lồng nhau • Vídụ: if (…) else {if(…) else {…}}
Vídụ if • Vídụnàydùngđể so sánh 2 số <?php $a=7; $b=9; if ($a==b) echo "a và b bằng nhau"; else if($a>$b) echo "a lớn hơn b"; else echo "a nhỏ hơn b"; ?>
Switch case switch (exp) { case giá_trị 1 {khốilệnh 1; break;} case giá_trị 2 {khốilệnh 2; break;} … case giá_trị n {khốilệnh n; break;} default: {khốilệnh n+1; break;} }
Vídụ switch…case switch($thu) { case 1: {echo "Chủ nhật";break;} case 2: {echo "Thứ 2";break;} case 3: {echo "Thứ 3";break;} case 4: {echo "Thứ 4";break;} case 5: {echo "Thứ 5";break;} case 6: {echo "Thứ 6";break;} case 7: {echo "Thứ 7";break;} default: echo "Thứ không đúng"; }
Vònglặp while • Cúpháp while(điềukiệnlặp) { khốilệnh } • Vídụ: $i=0; while($i<10) echo $i++. “ “; //xuấtcácsốtừ 0 đến 9
Vònglặp do … while • Cúpháp do { khối lệnh } while(điềukiệnlặp) • Vídụ: $i=0; do { echo $i++. “ “;} //xuấtcácsốtừ 0 đến 9 while($i<10)
Truy xuất mảng • Giả sử truy xuất mảng $a có các cách sau: • for($i=0; $i<count($a);$++) {//khối lệnh xử lý} • foreach($a as $value) {} • foreach($a as $key=>$value) {} • while(list($key,$value) =each($a)){}
$_POST • $_POST dùngđểtruyxuấtgiátrịcủacácđốitượngnhậpliệutrên form submit trướcđógửitớibằngphươngthức POST Xinchào An //echo $_POST[‘txtName ‘] An POST txtName form.html
$_GET • $_POST dùngđểtruyxuấtgiátrịcácthamsốcủaquerystring Xinchào An //echo $_GET[‘ten ‘] An …/xuly.php?ten=An GET form.html