端端舝

端端舝Share a Scribd company logo
L?p tr足nh v角 Thi?t k? Web 1
? 2007 Khoa C?ng ngh? th?ng tin
Khoa CNTT 每 ?H.KHTN
Ba?i 7
PHP C? b?n
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
N?i dung
? Gi?i thi?u PHP
? C? ch? ho?t ??ng c?a WebServer
? C迆 ph芍p & Quy ??c trong PHP
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
N?i dung
? Gi?i thi?u PHP
? C? ch? ho?t ??ng c?a WebServer
? C迆 ph芍p & Quy ??c trong PHP
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Gi?i thi?u v? PHP 每 L?ch s? ph芍t tri?n
? 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, khai b芍o bi?n, m?ng, h角m ?? quy, c?u ?i?u ki?n, bi?u th?c, #
? PHP 3 (1998) : H? tr? ODBC, ?a h? ?i?u h角nh, giao th?c email (SNMP, IMAP),
b? ph?n t赤ch m? PHP (parser) c?a Zeev Suraski v角 Andi Gutmans
? PHP 4 (2000) : Tr? th角nh m?t th角nh ph?n ??c l?p cho c芍c webserver. Parse ??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
? Phi那n b?n m?i nh?t c?a PHP l角 version PHP 5.2.4 (www.php.net)
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Gi?i thi?u v? PHP 每 PHP l角 g足 ?
? PHP vi?t t?t c?a PHP Hypertext Preprocessor
? L角 ng?n ng? server-side script, t??ng t? nh? ASP,
JSP, # th?c thi ? ph赤a WebServer
? T?p tin PHP c車 ph?n m? r?ng l角 .php
? C迆 ph芍p ng?n ng? gi?ng ng?n ng? C & Perl
?u ?i?m
PHP ?
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Gi?i thi?u v? PHP 每 ?u ?i?m 1
? PHP ???c s? d?ng l角m
每 Server Side Scripting
每 CommandLine Scripting (cron 每 Linux, Task Scheduler 每
Windows, Text Processing)
每 X?y d?ng ?ng Desktop 每 PHP GTK
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Gi?i thi?u v? PHP 每 ?u ?i?m 2
? ?a m?i tr??ng (Multi-Platform)
每 Web Servers: Apache, Microsoft IIS, Caudium, Netscape
Enterprise Server
每 H? ?i?u h角nh: UNIX (HP-UX, OpenBSD, Solaris, Linux), Mac
OSX, Windows NT/98/2000/XP/2003/vista
每 H? QTCSDL: Adabas D, dBase,Empress, FilePro (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
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Gi?i thi?u v? PHP 每 ?u ?i?m 3
? Mi?n ph赤
PHP
Software Free
Platform
Free (Linux)
Development Tools Free (PHP Coder, jEdit, #)
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Gi?i thi?u v? PHP 每 ?u ?i?m 4
? ???c s? d?ng r?ng r?i trong m?i tr??ng ph芍t tri?n web
每 20,917,850 domains (chi?m h?n 32% t那n mi?n website)
每 1,224,183 IP addresses
(04/2007 Netcraft Survey 每 http://www.php.net/usage.php)
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Gi?i thi?u v? PHP 每 M?t s? website l?n
PHP at Yahoo!
http://www.yahoo.com
The Internet*s most trafficked site
Portal
Wiki
Course Management System
Portal
Bulletin Board Content Management System
Customer Relationship Management
Help Desk
e-Commerce
Portal
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Gi?i thi?u v? PHP 每 C?n g足 ?? ch?y PHP ?
? Download PHP
每 Download PHP for free here:
http://www.php.net/downloads.php
? Download MySQL Database
每 Download MySQL for free here:
http://www.mysql.com/downloads/index.html
? Download Apache Server
每 Download Apache for free here:
http://httpd.apache.org/download.cgi
? Download WAMP,LAMP
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
N?i dung
? Gi?i thi?u PHP
? C? ch? ho?t ??ng c?a WebServer
? C迆 ph芍p & Quy ??c trong PHP
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
C? ch? ho?t ??ng c?a WebServer
2
Internet
or Intranet
www.example.com
Webserver Apache
or IIS
ServerSide Script
Parser
(PHP, ASP, ..)
Database
Server
Disk
driver
3
45
67
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
C? ch? ho?t ??ng c?a WebServer
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
C? ch? ho?t ??ng c?a WebServer
? Parser.asp ? Parser.php
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
N?i dung
? Gi?i thi?u PHP
? C? ch? ho?t ??ng c?a WebServer
? C迆 ph芍p & Quy ??c trong PHP
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
C迆 ph芍p & Quy ??c trong PHP
? Quy ??c
? Khai b芍o bi?n
? Ki?u d? li?u
? To芍n t?
? C?u tr迆c ?i?u khi?n
? H角m
? L?p ??i t??ng
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Quy ??c
? M? l?nh PHP ???c ??t trong c芍c c?p th? sau :
Th? m? Th? ?車ng
<? ?>
<?php ?>
<script language="php"> <script>
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Quy ??c
? T?t c? c芍c c?u l?nh php ??u c芍ch nhau b?i d?u ※;§
? Kh?ng ph?n bi?t kho?ng tr?ng, Tab, xu?ng d辰ng trong c?u l?nh
<?php print "Hello"; print " World!"; ?>
<?php
Print ※Hello§ ;
print ※ World!§;
?>
? Ghi ch迆 : Theo c迆 ph芍p ghi ch迆 c?a C++ & Perl
// ??y l角 ghi ch迆
# ??y l角 ghi ch迆
/* ??y l角 ghi
ch迆 nhi?u d辰ng*/
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
C迆 ph芍p & Quy ??c trong PHP
? Quy ??c
? Khai b芍o bi?n
? Ki?u d? li?u
? To芍n t?
? C?u tr迆c ?i?u khi?n
? H角m
? L?p ??i t??ng
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Khai b芍o bi?n
$ten_bien = value;
? Kh?ng khai b芍o ki?u d? li?u
? Bi?n t? ??ng ???c kh?i t?o ? l?n ??u ti那n g芍n gi芍 tr? cho bi?n
? T那n bi?n :
每 C車 th? bao g?m c芍c K? t? (A..Z, a..z), K? s? (0..9), _, $
每 Kh?ng ???c b?t d?u b?ng k? s? (0..9)
每 Ph?n bi?t ch? hoa 每 ch? th??ng
V赤 d? :
$size $my_drink_size $_drinks $drink4you
$$2hot4u $drink-size x
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Khai b芍o bi?n
? Variable variables
每 Cho ph谷p thay ??i t那n bi?n
每 V赤 d?:
$varname = ※Bien_moi§;
$$varname = ※xyz§; // $Bien_moi = ※xyz§
? H?ng s? - Constants
每 V赤 d?:
define(※MY_CONST§, 10);
echo MY_CONST;
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
C迆 ph芍p & Quy ??c trong PHP
? Quy ??c
? Khai b芍o bi?n
? Ki?u d? li?u
? To芍n t?
? C?u tr迆c ?i?u khi?n
? H角m
? L?p ??i t??ng
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Ki?u d? li?u
? boolean (bool)
? integer (int)
? double (float, real)
? string
? array
? object
1 Bi?n trong PHP c車 th? l?u b?t k? ki?u d? li?u n角o.
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Ki?u d? li?u (tt)
? Chuy?n ki?u d? li?u
每 C芍ch 1 (automatic)
$var = "100" + 15;
$var = "100" + 15.0;
$var = 39 . " Steps";
每 C芍ch 2: (datatype) $var
每 C芍ch 3: settype($var, ※datatype§)
$var (int)$var (bool)$var (string)$var
null 0 false ※§
true 1 ※1§
false 0 ※§
※6 feet§ 6 true
※foo§ 0 true
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Ki?u d? li?u (tt)
? Ki?m tra ki?u d? li?u
gettype is_string isset
is_integer is_array unset
is_double is_object empty
V赤 d?:
$var = "test";
if (isset($var))
echo "Variable is Set";
if (empty($var))
echo "Variable is Empty";
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Ki?u s? - int, float
? M?t s? h角m x? l? s?
每 abs pow decbin srand(seed)
每 ceil sqrt bindec rand
每 floor log dechex rand(min, max)
每 round log10 hexdec #
? V赤 d?
// Ph芍t sinh m?t ※m?m§ ng?u nhi那n
$seed = (float) microtime( ) * 100000000;
// Kh?i t?o b? ph芍t sinh s? ng?u nhi那n
srand($seed);
// In con s? ng?u nhi那n
print rand(); // Gi芍 tr? ng?u nhi那n t? 0 ??n getmaxrand( )
print rand(1, 6); // Gi芍 tr? ng?u nhi那n t? 1 ??n 6
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
Ki?u chu?i - string
? To芍n t? n?i chu?i : d?u ch?m .
$s = ※Hello§ . ※ World§; // $s = ※Hello World§
? Ph?n bi?t d?u nh芍y ??n v角 nh芍y k谷p
$user = ※Bill§;
print ?Hi $user?; // Hi $user
print ※Hi $user§; // Hi Bill
print ?Hi? . $user; // ????
print ?Hi? . ?$user?; // ????
? M?t s? h角m x? l? chu?i
每 printf trim strtolower
每 str_pad str_replace strtoupper
每 strlen substr strcasecmp
每 #
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
V赤 d?
<?
$tax = 0.075;
printf('The tax costs $%.2f', $tax);
$zip = '6520';
printf("ZIP is %05d§, $zip);
$min = -40; $max = 40;
printf("The computer can operate between %+d and %+d
degrees Celsius.", $min, $max);
?>
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
V赤 d?
<?
echo ※<pre>§
// Print a heading
echo str_pad(※Dramatis Personae", 50, " ", STR_PAD_BOTH) . ※n";
// Print an index line
echo str_pad(※DUNCAN, king of Scotland§, 30, ".")
. str_pad(※Larry§, 20, ".", STR_PAD_LEFT)
. ※n";
echo ※</pre>§
?>
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
M?ng - array
? Numbered array
$words = array("Web", "Database", "Applications");
echo $words[0];
$numbers = array(1=>※one§,※two§,※three§, "four");
echo $numbers[1];
? Associated array
$array = array("first"=>1, "second"=>2, "third"=>3);
echo $array["second"];
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
M?ng - array
? M?t s? h角m x? l? tr那n m?ng
每 Count in_array sort asort ksort usort
每 min array_reverse rsort arsort krsprt uasort
每 max uksort
? V赤 d?:
$dinner = array(
'Sweet Corn and Asparagus',
'Lemon Chicken',
'Braised Bamboo Fungus');
sort($dinner);
print "I want $dinner[0] and $dinner[1].";
$dishes = count($dinner);
print $dishes;
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
M?ng - array
? M?t s? h角m li那n quan ??n m?ng
每 reset(array)
每 array_push(array, elements) : Th那m elements v角o cu?i m?ng
每 array_pop(array) : L?y ph?n t? cu?i ra kh?i m?ng
每 array_unshift(array, elements) : Th那m elements v角o ??u m?ng
每 array_shift(array) : L?y ph?n t? ??u ra kh?i m?ng
每 array_merge(array, array) : k?t 2 m?ng l?i v角 tr? ra m?ng m?i
每 shuffle(array) : Sort random m?ng
每 sort(array, flag) : flag = {sort_regular, sort_numeric, sort_string, sort_locale_string}
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
C迆 ph芍p & Quy ??c trong PHP
? Quy ??c
? Khai b芍o bi?n
? Ki?u d? li?u
? To芍n t?
? C?u tr迆c ?i?u khi?n
? H角m
? L?p ??i t??ng
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
To芍n t?
Lo?i To芍n t? Ghi ch迆
new
. [ ] ( )
To芍n h?c + - * / % ++ --
So s芍nh < > <= >= != == === !==
Lu?n l? && || ?: ,
X? l? bit ! ~ << >> >>>
AND OR XOR
G芍n = += -= *= /= %=
>>= <<= &= |= ^= .=
?p ki?u (ki?u d? li?u) (int) (double) (string)#
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
C迆 ph芍p & Quy ??c trong PHP
? Quy ??c
? Khai b芍o bi?n
? Ki?u d? li?u
? To芍n t?
? C?u tr迆c ?i?u khi?n
? H角m
? L?p ??i t??ng
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
C?u tr迆c ?i?u khi?n
? ?i?u ki?n if
? ?i?u khi?n switch
? V辰ng l?p for
? V辰ng l?p while
? V辰ng l?p do.. While
? V辰ng l?p foreach
? T? kh車a break, continue
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
?i?u ki?n if
if (condition)
{
statement[s] if true
}
else (condition)
{
statement[s] if false
}
V赤 d?:
$x = 5;
if ($x < 4)
echo ※$x is less than 4§;
else
print ?$x isn?t less than 4?;
$x isn*t less than 4
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
?i?u khi?n switch
switch (expression)
{
case label :
statementlist
break;
case label :
statementlist
break;
...
default :
statementlist
}
V赤 d?:
$menu = 3;
switch ($menu){
case 1:
echo "You picked one";
break;
case 2:
echo "You picked two";
break;
case 3:
echo "You picked three";
case 4:
echo "You picked four";
break;
default:
echo "You picked another option";
}
You picked three You picked four
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
V辰ng l?p for
for ([initial expression]; [condition]; [update expression])
{
statement[s] inside loop
}
? V赤 d?:
print ※<select>§;
for ($i = 1; $i <= 12; $i++)
{
print ※<option>$i</option>§;
}
print ※</select>§;
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
V辰ng l?p while, do#while
while (expression)
{
statements
}
do
{
statements
}while (expression);
V赤 d?:
$i = 1; $j = 9;
while ($i <= 10)
{
$temp = $i * $j;
print ※$j * $i = $temp<br>§;
$i++;
}
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
V辰ng l?p foreach
foreach (array as variable)
{
statements
}
V赤 d?:
$meal = array('breakfast' => 'Walnut Bun',
'lunch' => 'Cashew Nuts and White Mushrooms',
'dinner' => 'Eggplant with Chili Sauce');
print "<table border=?1?>n";
foreach ($meal as $key => $value)
{
print "<tr><td>$key</td><td>$value</td></tr>n";
}
print '</table>';
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
C迆 ph芍p & Quy ??c trong PHP
? Quy ??c
? Khai b芍o bi?n
? Ki?u d? li?u
? To芍n t?
? C?u tr迆c ?i?u khi?n
? H角m
? L?p ??i t??ng
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
H角m - function
function functionName ([parameter1]...[,parameterN])
{
statement[s] ;
}
function functionName ([parameter1]...[,parameterN])
{
statement[s] ;
return #.. ;
}
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
H角m 每 Ph?m vi bi?n
<?php
function doublevalue($var=5)
{
global $temp;
$temp = $var * 2;
}
$temp = 5;
doublevalue();
echo "$temp is: $temp";
?>
$temp = 5
$temp = 10
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
H角m 每 Tham tr? vs Tham bi?n
<?php
function doublevalue( $var)
{
$var = $var * 2;
}
$variable = 5;
doublevalue($variable);
echo "$variable is: $variable";
?>
$variable = 5
$variable =10
&
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
H角m 每 include & require
// functions.inc
<?php
function bold($string)
{
echo "<b>" . $string . "</b>n";
}
?>
// index.php
<html>
<head>
<title>Simple Function Call</title>
</head>
<body bgcolor="#ffffff">
<?
include "functions.inc";
bold("this is bold");
$myString = "this is bold";
bold($myString);
?>
</body></html>
require "functions.inc";
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
C迆 ph芍p & Quy ??c trong PHP
? Quy ??c
? Khai b芍o bi?n
? Ki?u d? li?u
? To芍n t?
? C?u tr迆c ?i?u khi?n
? H角m
? L?p ??i t??ng
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
L?p ??i t??ng - class
class class_name() [extends superclass_name]
{
var $attribute;
#
function method_name()
{
$this->attribute = #;
}
#
}
$a = new class_name(#);
L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n
? 2007 Khoa CNTT - ?HKHTN
L?p ??i t??ng - class
? V赤 d?:
class Counter {
var $count = 0;
var $startPoint = 0;
function increment( ) {
$this->count++;
}
}
$aCounter = new Counter;
$aCounter->increment( );
echo $aCounter->count; // prints 1
$aCounter->count = 101;

More Related Content

Web course php co ban

  • 1. L?p tr足nh v角 Thi?t k? Web 1 ? 2007 Khoa C?ng ngh? th?ng tin Khoa CNTT 每 ?H.KHTN Ba?i 7 PHP C? b?n
  • 2. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN N?i dung ? Gi?i thi?u PHP ? C? ch? ho?t ??ng c?a WebServer ? C迆 ph芍p & Quy ??c trong PHP
  • 3. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN N?i dung ? Gi?i thi?u PHP ? C? ch? ho?t ??ng c?a WebServer ? C迆 ph芍p & Quy ??c trong PHP
  • 4. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Gi?i thi?u v? PHP 每 L?ch s? ph芍t tri?n ? 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, khai b芍o bi?n, m?ng, h角m ?? quy, c?u ?i?u ki?n, bi?u th?c, # ? PHP 3 (1998) : H? tr? ODBC, ?a h? ?i?u h角nh, giao th?c email (SNMP, IMAP), b? ph?n t赤ch m? PHP (parser) c?a Zeev Suraski v角 Andi Gutmans ? PHP 4 (2000) : Tr? th角nh m?t th角nh ph?n ??c l?p cho c芍c webserver. Parse ??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 ? Phi那n b?n m?i nh?t c?a PHP l角 version PHP 5.2.4 (www.php.net)
  • 5. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Gi?i thi?u v? PHP 每 PHP l角 g足 ? ? PHP vi?t t?t c?a PHP Hypertext Preprocessor ? L角 ng?n ng? server-side script, t??ng t? nh? ASP, JSP, # th?c thi ? ph赤a WebServer ? T?p tin PHP c車 ph?n m? r?ng l角 .php ? C迆 ph芍p ng?n ng? gi?ng ng?n ng? C & Perl ?u ?i?m PHP ?
  • 6. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Gi?i thi?u v? PHP 每 ?u ?i?m 1 ? PHP ???c s? d?ng l角m 每 Server Side Scripting 每 CommandLine Scripting (cron 每 Linux, Task Scheduler 每 Windows, Text Processing) 每 X?y d?ng ?ng Desktop 每 PHP GTK
  • 7. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Gi?i thi?u v? PHP 每 ?u ?i?m 2 ? ?a m?i tr??ng (Multi-Platform) 每 Web Servers: Apache, Microsoft IIS, Caudium, Netscape Enterprise Server 每 H? ?i?u h角nh: UNIX (HP-UX, OpenBSD, Solaris, Linux), Mac OSX, Windows NT/98/2000/XP/2003/vista 每 H? QTCSDL: Adabas D, dBase,Empress, FilePro (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
  • 8. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Gi?i thi?u v? PHP 每 ?u ?i?m 3 ? Mi?n ph赤 PHP Software Free Platform Free (Linux) Development Tools Free (PHP Coder, jEdit, #)
  • 9. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Gi?i thi?u v? PHP 每 ?u ?i?m 4 ? ???c s? d?ng r?ng r?i trong m?i tr??ng ph芍t tri?n web 每 20,917,850 domains (chi?m h?n 32% t那n mi?n website) 每 1,224,183 IP addresses (04/2007 Netcraft Survey 每 http://www.php.net/usage.php)
  • 10. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Gi?i thi?u v? PHP 每 M?t s? website l?n PHP at Yahoo! http://www.yahoo.com The Internet*s most trafficked site Portal Wiki Course Management System Portal Bulletin Board Content Management System Customer Relationship Management Help Desk e-Commerce Portal
  • 11. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Gi?i thi?u v? PHP 每 C?n g足 ?? ch?y PHP ? ? Download PHP 每 Download PHP for free here: http://www.php.net/downloads.php ? Download MySQL Database 每 Download MySQL for free here: http://www.mysql.com/downloads/index.html ? Download Apache Server 每 Download Apache for free here: http://httpd.apache.org/download.cgi ? Download WAMP,LAMP
  • 12. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN N?i dung ? Gi?i thi?u PHP ? C? ch? ho?t ??ng c?a WebServer ? C迆 ph芍p & Quy ??c trong PHP
  • 13. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN C? ch? ho?t ??ng c?a WebServer 2 Internet or Intranet www.example.com Webserver Apache or IIS ServerSide Script Parser (PHP, ASP, ..) Database Server Disk driver 3 45 67
  • 14. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN C? ch? ho?t ??ng c?a WebServer
  • 15. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN C? ch? ho?t ??ng c?a WebServer ? Parser.asp ? Parser.php
  • 16. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN N?i dung ? Gi?i thi?u PHP ? C? ch? ho?t ??ng c?a WebServer ? C迆 ph芍p & Quy ??c trong PHP
  • 17. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN C迆 ph芍p & Quy ??c trong PHP ? Quy ??c ? Khai b芍o bi?n ? Ki?u d? li?u ? To芍n t? ? C?u tr迆c ?i?u khi?n ? H角m ? L?p ??i t??ng
  • 18. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Quy ??c ? M? l?nh PHP ???c ??t trong c芍c c?p th? sau : Th? m? Th? ?車ng <? ?> <?php ?> <script language="php"> <script>
  • 19. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Quy ??c ? T?t c? c芍c c?u l?nh php ??u c芍ch nhau b?i d?u ※;§ ? Kh?ng ph?n bi?t kho?ng tr?ng, Tab, xu?ng d辰ng trong c?u l?nh <?php print "Hello"; print " World!"; ?> <?php Print ※Hello§ ; print ※ World!§; ?> ? Ghi ch迆 : Theo c迆 ph芍p ghi ch迆 c?a C++ & Perl // ??y l角 ghi ch迆 # ??y l角 ghi ch迆 /* ??y l角 ghi ch迆 nhi?u d辰ng*/
  • 20. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN C迆 ph芍p & Quy ??c trong PHP ? Quy ??c ? Khai b芍o bi?n ? Ki?u d? li?u ? To芍n t? ? C?u tr迆c ?i?u khi?n ? H角m ? L?p ??i t??ng
  • 21. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Khai b芍o bi?n $ten_bien = value; ? Kh?ng khai b芍o ki?u d? li?u ? Bi?n t? ??ng ???c kh?i t?o ? l?n ??u ti那n g芍n gi芍 tr? cho bi?n ? T那n bi?n : 每 C車 th? bao g?m c芍c K? t? (A..Z, a..z), K? s? (0..9), _, $ 每 Kh?ng ???c b?t d?u b?ng k? s? (0..9) 每 Ph?n bi?t ch? hoa 每 ch? th??ng V赤 d? : $size $my_drink_size $_drinks $drink4you $$2hot4u $drink-size x
  • 22. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Khai b芍o bi?n ? Variable variables 每 Cho ph谷p thay ??i t那n bi?n 每 V赤 d?: $varname = ※Bien_moi§; $$varname = ※xyz§; // $Bien_moi = ※xyz§ ? H?ng s? - Constants 每 V赤 d?: define(※MY_CONST§, 10); echo MY_CONST;
  • 23. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN C迆 ph芍p & Quy ??c trong PHP ? Quy ??c ? Khai b芍o bi?n ? Ki?u d? li?u ? To芍n t? ? C?u tr迆c ?i?u khi?n ? H角m ? L?p ??i t??ng
  • 24. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Ki?u d? li?u ? boolean (bool) ? integer (int) ? double (float, real) ? string ? array ? object 1 Bi?n trong PHP c車 th? l?u b?t k? ki?u d? li?u n角o.
  • 25. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Ki?u d? li?u (tt) ? Chuy?n ki?u d? li?u 每 C芍ch 1 (automatic) $var = "100" + 15; $var = "100" + 15.0; $var = 39 . " Steps"; 每 C芍ch 2: (datatype) $var 每 C芍ch 3: settype($var, ※datatype§) $var (int)$var (bool)$var (string)$var null 0 false ※§ true 1 ※1§ false 0 ※§ ※6 feet§ 6 true ※foo§ 0 true
  • 26. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Ki?u d? li?u (tt) ? Ki?m tra ki?u d? li?u gettype is_string isset is_integer is_array unset is_double is_object empty V赤 d?: $var = "test"; if (isset($var)) echo "Variable is Set"; if (empty($var)) echo "Variable is Empty";
  • 27. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Ki?u s? - int, float ? M?t s? h角m x? l? s? 每 abs pow decbin srand(seed) 每 ceil sqrt bindec rand 每 floor log dechex rand(min, max) 每 round log10 hexdec # ? V赤 d? // Ph芍t sinh m?t ※m?m§ ng?u nhi那n $seed = (float) microtime( ) * 100000000; // Kh?i t?o b? ph芍t sinh s? ng?u nhi那n srand($seed); // In con s? ng?u nhi那n print rand(); // Gi芍 tr? ng?u nhi那n t? 0 ??n getmaxrand( ) print rand(1, 6); // Gi芍 tr? ng?u nhi那n t? 1 ??n 6
  • 28. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN Ki?u chu?i - string ? To芍n t? n?i chu?i : d?u ch?m . $s = ※Hello§ . ※ World§; // $s = ※Hello World§ ? Ph?n bi?t d?u nh芍y ??n v角 nh芍y k谷p $user = ※Bill§; print ?Hi $user?; // Hi $user print ※Hi $user§; // Hi Bill print ?Hi? . $user; // ???? print ?Hi? . ?$user?; // ???? ? M?t s? h角m x? l? chu?i 每 printf trim strtolower 每 str_pad str_replace strtoupper 每 strlen substr strcasecmp 每 #
  • 29. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN V赤 d? <? $tax = 0.075; printf('The tax costs $%.2f', $tax); $zip = '6520'; printf("ZIP is %05d§, $zip); $min = -40; $max = 40; printf("The computer can operate between %+d and %+d degrees Celsius.", $min, $max); ?>
  • 30. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN V赤 d? <? echo ※<pre>§ // Print a heading echo str_pad(※Dramatis Personae", 50, " ", STR_PAD_BOTH) . ※n"; // Print an index line echo str_pad(※DUNCAN, king of Scotland§, 30, ".") . str_pad(※Larry§, 20, ".", STR_PAD_LEFT) . ※n"; echo ※</pre>§ ?>
  • 31. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN M?ng - array ? Numbered array $words = array("Web", "Database", "Applications"); echo $words[0]; $numbers = array(1=>※one§,※two§,※three§, "four"); echo $numbers[1]; ? Associated array $array = array("first"=>1, "second"=>2, "third"=>3); echo $array["second"];
  • 32. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN M?ng - array ? M?t s? h角m x? l? tr那n m?ng 每 Count in_array sort asort ksort usort 每 min array_reverse rsort arsort krsprt uasort 每 max uksort ? V赤 d?: $dinner = array( 'Sweet Corn and Asparagus', 'Lemon Chicken', 'Braised Bamboo Fungus'); sort($dinner); print "I want $dinner[0] and $dinner[1]."; $dishes = count($dinner); print $dishes;
  • 33. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN M?ng - array ? M?t s? h角m li那n quan ??n m?ng 每 reset(array) 每 array_push(array, elements) : Th那m elements v角o cu?i m?ng 每 array_pop(array) : L?y ph?n t? cu?i ra kh?i m?ng 每 array_unshift(array, elements) : Th那m elements v角o ??u m?ng 每 array_shift(array) : L?y ph?n t? ??u ra kh?i m?ng 每 array_merge(array, array) : k?t 2 m?ng l?i v角 tr? ra m?ng m?i 每 shuffle(array) : Sort random m?ng 每 sort(array, flag) : flag = {sort_regular, sort_numeric, sort_string, sort_locale_string}
  • 34. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN C迆 ph芍p & Quy ??c trong PHP ? Quy ??c ? Khai b芍o bi?n ? Ki?u d? li?u ? To芍n t? ? C?u tr迆c ?i?u khi?n ? H角m ? L?p ??i t??ng
  • 35. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN To芍n t? Lo?i To芍n t? Ghi ch迆 new . [ ] ( ) To芍n h?c + - * / % ++ -- So s芍nh < > <= >= != == === !== Lu?n l? && || ?: , X? l? bit ! ~ << >> >>> AND OR XOR G芍n = += -= *= /= %= >>= <<= &= |= ^= .= ?p ki?u (ki?u d? li?u) (int) (double) (string)#
  • 36. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN C迆 ph芍p & Quy ??c trong PHP ? Quy ??c ? Khai b芍o bi?n ? Ki?u d? li?u ? To芍n t? ? C?u tr迆c ?i?u khi?n ? H角m ? L?p ??i t??ng
  • 37. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN C?u tr迆c ?i?u khi?n ? ?i?u ki?n if ? ?i?u khi?n switch ? V辰ng l?p for ? V辰ng l?p while ? V辰ng l?p do.. While ? V辰ng l?p foreach ? T? kh車a break, continue
  • 38. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN ?i?u ki?n if if (condition) { statement[s] if true } else (condition) { statement[s] if false } V赤 d?: $x = 5; if ($x < 4) echo ※$x is less than 4§; else print ?$x isn?t less than 4?; $x isn*t less than 4
  • 39. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN ?i?u khi?n switch switch (expression) { case label : statementlist break; case label : statementlist break; ... default : statementlist } V赤 d?: $menu = 3; switch ($menu){ case 1: echo "You picked one"; break; case 2: echo "You picked two"; break; case 3: echo "You picked three"; case 4: echo "You picked four"; break; default: echo "You picked another option"; } You picked three You picked four
  • 40. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN V辰ng l?p for for ([initial expression]; [condition]; [update expression]) { statement[s] inside loop } ? V赤 d?: print ※<select>§; for ($i = 1; $i <= 12; $i++) { print ※<option>$i</option>§; } print ※</select>§;
  • 41. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN V辰ng l?p while, do#while while (expression) { statements } do { statements }while (expression); V赤 d?: $i = 1; $j = 9; while ($i <= 10) { $temp = $i * $j; print ※$j * $i = $temp<br>§; $i++; }
  • 42. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN V辰ng l?p foreach foreach (array as variable) { statements } V赤 d?: $meal = array('breakfast' => 'Walnut Bun', 'lunch' => 'Cashew Nuts and White Mushrooms', 'dinner' => 'Eggplant with Chili Sauce'); print "<table border=?1?>n"; foreach ($meal as $key => $value) { print "<tr><td>$key</td><td>$value</td></tr>n"; } print '</table>';
  • 43. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN C迆 ph芍p & Quy ??c trong PHP ? Quy ??c ? Khai b芍o bi?n ? Ki?u d? li?u ? To芍n t? ? C?u tr迆c ?i?u khi?n ? H角m ? L?p ??i t??ng
  • 44. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN H角m - function function functionName ([parameter1]...[,parameterN]) { statement[s] ; } function functionName ([parameter1]...[,parameterN]) { statement[s] ; return #.. ; }
  • 45. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN H角m 每 Ph?m vi bi?n <?php function doublevalue($var=5) { global $temp; $temp = $var * 2; } $temp = 5; doublevalue(); echo "$temp is: $temp"; ?> $temp = 5 $temp = 10
  • 46. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN H角m 每 Tham tr? vs Tham bi?n <?php function doublevalue( $var) { $var = $var * 2; } $variable = 5; doublevalue($variable); echo "$variable is: $variable"; ?> $variable = 5 $variable =10 &
  • 47. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN H角m 每 include & require // functions.inc <?php function bold($string) { echo "<b>" . $string . "</b>n"; } ?> // index.php <html> <head> <title>Simple Function Call</title> </head> <body bgcolor="#ffffff"> <? include "functions.inc"; bold("this is bold"); $myString = "this is bold"; bold($myString); ?> </body></html> require "functions.inc";
  • 48. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN C迆 ph芍p & Quy ??c trong PHP ? Quy ??c ? Khai b芍o bi?n ? Ki?u d? li?u ? To芍n t? ? C?u tr迆c ?i?u khi?n ? H角m ? L?p ??i t??ng
  • 49. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN L?p ??i t??ng - class class class_name() [extends superclass_name] { var $attribute; # function method_name() { $this->attribute = #; } # } $a = new class_name(#);
  • 50. L?p tr足nh v角 Thi?t k? Web 1 每 B角i 7 : PHP C? b?n ? 2007 Khoa CNTT - ?HKHTN L?p ??i t??ng - class ? V赤 d?: class Counter { var $count = 0; var $startPoint = 0; function increment( ) { $this->count++; } } $aCounter = new Counter; $aCounter->increment( ); echo $aCounter->count; // prints 1 $aCounter->count = 101;