Bài giảng Phát triển ứng dụng Web: Bài 1 PHP Cơ bản cung cấp cho người học những kiến thức như: Giới thiệu PHP; Cơ chế hoạt động của WebServer; Cú pháp và Quy ước trong PHP. Mời các bạn cùng tham khảo!
Trang 2Nội dung
▪ Giới thiệu PHP
▪ Cơ chế hoạt động của WebServer
▪ Cú pháp & Quy ước trong PHP
Trang 3Giới thiệu về PHP
− PHP : Rasmus Lerdorf in 1994 (được phát triển để phát sinh các form
đăng nhập sử dụng giao thức HTTP của Unix)
− PHP 2 (1995) : Chuyển sang ngôn ngữ script xử lý trên server Hỗ trợ
CSDL, Upload File, biến, mảng, hàm đệ quy, câu điều kiện, …
− PHP 3 (1998) : Hỗ trợ ODBC, đa hệ điều hành , giao thức email
(SNMP, IMAP)
− PHP 4 (2000) : Parser đổi tên thành Zend Engine Bổ sung các tính
năng bảo mật cho PHP
− PHP 5 (2005) : Bổ sung Zend Engine II hỗ trợ lập trình HĐT , XML ,
SOAP cho Web Services, SQLite
− PHP 7 (2015): Cải thiện hiệu năng, tính năng ngôn ngữ mới
Trang 4Giới thiệu về PHP – Ưu điểm 1
programming language we know (https://w3techs.com/technologies/details/pl-php/all/all)
Popular sites using PHP
Trang 5Giới thiệu về PHP – Ưu điểm 2
Trang 6Disk driver
3
4 5
6 7
Trang 8PHP
Solid, PostgreSQL, SQL Server,…)
Trang 9PHP
Trang 10PHP + MySQL
trong Windows, Unix
❖Why PHP?
Trang 11❖Download MySQL Database
❖Download Apache Server
− Download WAMP, XAMPP
Trang 12PHP Syntax
PHP
Trang 13Comments in PHP
khối comment
Trang 14PHP Variables
Trang 15PHP is a Loosely Typed Language
nó
Trang 16Naming Rules for Variables
A-Z, 0-9, and _ )
($my_string), hoặc với chữ hoa ($myString)
Trang 17PHP String Variables
Trang 18echo and print
echo or echo().
echo "Hello world!<br>";
print "I'm about to learn PHP!";
Trang 19Data Types
types can do different things
Trang 20The Concatenation Operator
Trang 21The strlen() function
Trang 22The strpos() function
▪ Nếu kết quả được tìm thấy, hàm trả về vị trí của ký tự đầu tiên
▪ Nếu không tìm thấy, nó sẽ trả về FALSE.
Trang 23Lập trình ứng dụng mạng 23
PHP Operators
❖Arithmetic Operators
Trang 24PHP Operators
❖Assignment Operators
Trang 25PHP Operators
❖Comparison Operators
Trang 26PHP Operators
❖Logical Operators
Trang 27Conditional Statements
khác nhau dựa trên các điều kiện khác nhau
Trang 29The if else Statement
lệnh khác nếu điều kiện sai
❖Syntax
❖Example
Trang 30The if elseif else Statement
❖Syntax
❖Example
Trang 31The PHP Switch Statement
nhau
❖Syntax Syntax
Trang 32The PHP Switch Statement
Trang 34The while loop
− Thực thi một khối mã lệnh trong khi một điều kiện là đúng.
Trang 35The while loop
Trang 36The do while Statement
lặp lại vòng lặp trong khi điều kiện là đúng
Trang 37The do while Statement
Trang 38For Loops
đến khi một điều kiện quy định là đúng
❖Syntax
▪ init: thiết lập một giá trị khởi tạo
▪ condition: đkiện lặp Nếu đk là TRUE, vòng lặp tiếp tục Nếu đk
để FALSE, vòng lặp kết thúc.
▪ increment: bước tăng tiếp theo của biến lặp
Trang 39For Loops
Trang 40foreach
❖Syntax
Trang 41The foreach loop
Trang 43PHP Functions
Trang 44PHP Functions - Adding parameters
Trang 45PHP Functions - Adding parameters
Trang 46PHP Functions - Return values
Trang 47PHP Arrays
− Một mảng lưu trữ nhiều giá trị trong một biến duy nhất.
− Mỗi phần tử trong mảng có chỉ số riêng để truy cập
− 3 kiểu mảng:
▪ Indexed arrays - Arrays with a numeric index
▪ Associative arrays - Arrays with named keys
▪ Multidimensional arrays - Arrays containing one or more arrays
Trang 48Indexed Arrays
▪ Chỉ mục được gán tự động (chỉ số bắt đầu từ 0)
▪ Chỉ mục được gán bằng tay
Trang 49$cars = array( "Volvo" , "BMW" , "Toyota" );
echo "I like " $cars[ 0 ] ", " $cars[ 1 ] " and " $cars[ 2 ] "." ;
Trang 52Associative Arrays
❖Example 1: Sử dụng một mảng để phân định lứa tuổi
❖Example 2
Trang 53Associative Arrays - EX
<?php
$age = array( "Peter" => "35" , "Ben" => "37" , "Joe" => "43" );
echo "Peter is " $age[ 'Peter' ] " years old." ;
?>
Peter is 35 years old.
Trang 55Multidimensional Arrays
more arrays.
three, four, five, or more levels deep
▪ However, arrays more than three levels deep are hard to manage for most people.
Trang 56Multidimensional Arrays (Two-dimensional Arrays)
− A two-dimensional array is an array of arrays (a
three-dimensional array is an array of arrays of arrays).
− Look at the following table:
Trang 57Multidimensional Arrays (Two-dimensional Arrays)
▪ Get access to the elements of the $cars array
echo $cars[0][0] ": In stock: " $cars[0][1] ", sold: " $cars[0][2] ".<br>" ;
echo $cars[1][0] ": In stock: " $cars[1][1] ", sold: " $cars[1][2] ".<br>" ;
echo $cars[2][0] ": In stock: " $cars[2][1] ", sold: " $cars[2][2] ".<br>" ;
echo $cars[3][0] ": In stock: " $cars[3][1] ", sold: " $cars[3][2] ".<br>" ;
?>
Volvo: In stock: 22, sold: 18.
BMW: In stock: 15, sold: 13.
Saab: In stock: 5, sold: 2.
Land Rover: In stock: 17, sold: 15.
Trang 58Multidimensional Arrays (Two-dimensional Arrays)
▪ Loop to get the elements of the $cars array
<?php
//displaying array elements at once using for loop.
echo "<b>Array displayed using nested for loop:</b><br>" ;
for ( $r = 0 ; $r < count ( $cars ); $r ++ ){
for ( $c = 0 ; $c < count ( $ cars [ $r ]); $c ++ ){
echo $ cars [ $r ][ $c ] " " ; }
echo "<br>" ;
}
Trang 59Multidimensional Arrays (Two-dimensional Arrays)
▪ Loop to get the elements of the $cars array
<?php
for ($row = 0; $row < count ( $cars ) ; $row++) {
echo "<p><b>Row number $row</b></p>" ;
echo "<ul>" ;
for ($col = 0; $col < count ( $ cars [ $row ] ; $col++) {
echo "<li>" $cars[$row][$col] "</li>" ;
Trang 60Multidimensional Arrays
− Tạo một mảng đa chiều, với các ID gán tự động
Example: Hiển thị một giá trị từ
mảng ở trên:
Trang 61▪ sort() - sort arrays in ascending order
▪ rsort() - sort arrays in descending order
▪ asort() - sort associative arrays in ascending order, according to the
value
▪ ksort() - sort associative arrays in ascending order, according to the key
▪ arsort() - sort associative arrays in descending order, according to the value
▪ krsort() - sort associative arrays in descending order, according to the key
Trang 68PHP Forms and User Input
script
❖Example
Trang 69PHP Forms and User Input
file "welcome.php"
Trang 70PHP $_GET Function
▪ Có thể nhìn thấy tất cả mọi người (hiển thị trong thanh địa chỉ của trình duyệt)
▪ Có giới hạn về số lượng thông tin gửi.
Trang 72When to use method="get"?
biến và giá trị được hiển thị trong URL
− Lưu ý:
▪ Không nên sử dụng khi gửi mật khẩu hoặc các thông tin nhạy cảm khác!
▪ Không nên sử dụng khi giá trị vượt quá 2000 ký tự.
dấu trang Điều này có thể hữu ích trong một số trường hợp
Trang 73PHP $_POST Function
▪ Vô hình với người khác
▪ Không có giới hạn về số lượng thông tin gửi.
Trang 74PHP $_POST Function
Trang 76When to use method="post"?
▪ Vô hình cho người khác
▪ Không có giới hạn về số lượng thông tin để gửi.
thể đánh dấu trang
Trang 77Form Validation
processing PHP forms
important to protect your form
from hackers and spammers.
Field Validation Rules
Name Required + Must only contain letters and whitespace
E-mail Required + Must contain a valid email address (with @ and ) Comment Optional Multi-line input field (textarea)
Gender Required Must select one
The validation rules for the form above are as follows:
Trang 78Form Validation
− $_SERVER["PHP_SELF"] : a super global variable that
returns the filename of the currently executing script
▪ $_SERVER["PHP_SELF"] sends the submitted form data to the
page itself, instead of jumping to a different page
▪ This way, the user will get error messages on the same page as the form.
− htmlspecialchars() : function converts special characters to
HTML entities
▪ This means that it will replace HTML characters like < and > with < and >
▪ This prevents attackers from exploiting the code by injecting
HTML or Javascript code (Cross-site Scripting attacks) in forms.
Trang 79Form Validation
− Validate Form Data With PHP
▪ test_input() : check each $_POST variable
<?php
$name = $email = $gender = $comment = $website = "" ;
$name = test_input( $_POST [ "name" ]);
$email = test_input( $_POST [ "email" ]);
$comment = test_input( $_POST [ "comment" ]);
$gender = test_input( $_POST [ "gender" ]);
Trang 80< body >
<?php
$name = $email = $gender = $comment = $website = "" ;
if ( $_SERVER [ "REQUEST_METHOD" ] == "POST" ) {
$name = test_input( $_POST [ "name" ]);
$email = test_input( $_POST [ "email" ]);
$comment = test_input( $_POST [ "comment" ]);
$gender = test_input( $_POST [ "gender" ]);
< h2 > PHP Form Validation Example </ h2 >
< form method ="post" action =" <?php echo htmlspecialchars( $_SERVER [ "PHP_SELF" ]);?>" >
Name: < input type ="text" name = "name" > < br >< br >
E-mail: < input type ="text" name = "email" > < br >< br >
Comment:
< textarea name ="comment" rows = "5" cols = "40" ></ textarea >
< br >< br >
Gender:
< input type ="radio" name = "gender" value = "female" > Female
< input type ="radio" name = "gender" value = "male" > Male
< input type ="radio" name = "gender" value = "other" > Other < br >< br >
< input type ="submit" name = "submit" value = "Submit" >
Trang 81Form Validation
− Validate Form Data With PHP
▪ test_input() : check each $_POST variable