You are on page 1of 13

2

Lng V Minh lvminh@fit.hcmus.edu.vn


Trn Th Bch Hnh ttbhanh@fit.hcmus.edu.vn
2012 - International Training & Education Center (ITEC)
University of Science - Ho Chi Minh City
227 Nguyen Van Cu, HCM city
http://www.itec.hcmuns.edu.vn/

12/9/2013

PHP : Rasmus Lerdorf in 1994 (c pht trin pht sinh cc form ng nhp s

dng giao thc HTTP ca Unix)


PHP 2 (1995) : Chuyn sang ngn ng script x l trn server. H tr CSDL, Upload

File, khai bo bin, mng, hm quy, cu iu kin, biu thc,


PHP 3 (1998) : H tr ODBC, a h iu hnh, giao thc email (SNMP, IMAP), b

phn tch m PHP (parser) ca Zeev Suraski v Andi Gutmans


PHP 4 (2000) : Tr thnh mt thnh phn c lp cho cc webserver. Parse i tn

thnh Zend Engine. B sung cc tnh nng bo mt cho PHP


PHP 5 (2005) : B sung Zend Engine II h tr lp trnh HT, XML, SOAP cho Web

Services, SQLite
Phin bn mi nht ca PHP l version PHP 5.4.11 || 5.3.21 || 5.5.0

(www.php.net)

Software
Platform

Free
Free (Linux)

Development Tools

Free (PHP Coder, jEdit, )

10

http://www.php.net/usage.php

12/9/2013

11

12

www.example.com
Webserver

Apache
or IIS

Internet
or Intranet
7
ServerSide Script
Parser
(PHP, ASP, ..)

Database
Server

Disk
driver

12/9/2013

13

14

15

16

17

18

<?

?>

<?php

?>

<script language="php">

<script>

<?php print "Hello"; print " World!"; ?>


<?php
Print Hello
;
print World!;
?>

19

20

21

22

23

24

Cch 1 (automatic)
$var = "100" + 15;
$var = "100" + 15.0;
$var = 39 . " Steps";

Cch 2: (datatype) $var


Cch 3: settype($var, datatype)

$var

(int)$var

(bool)$var

(string)$var

null

false

true

false

6 feet

true

foo

true

25

26

abs
ceil
Floor
round

pow
sqrt
log
log10

decbin
bindec
dechex
hexdec

srand(seed)
rand
rand(min, max)

V d

$var = "test";
if (isset($var))
echo "Variable is Set";
if (empty($var))
echo "Variable is Empty";

27

28

<?
$tax = 0.075;
printf('The tax costs $%.2f', $tax);

printf

str_pad
strlen

trim
str_replace
substr

strtolower
strtoupper
strcasecmp

$zip = '6520';
printf("ZIP is %05d, $zip);
$min = -40; $max = 40;
printf("The computer can operate between %+d and %+d
degrees Celsius.", $min, $max);
?>

29

30

31

32

flag = {sort_regular, sort_numeric, sort_string, sort_locale_string}

33

34

35

36

37

38

V d:

if (condition)
{
statement[s] if true
}
else (condition)
{
statement[s] if false
}

switch (expression)

$menu = 3;
switch ($menu){
case 1:
echo "You picked
break;
case 2:
echo "You picked
break;
case 3:
echo "You picked
case 4:
echo "You picked
break;
default:
echo "You picked
option";
}

$x = 5;

case label :

if ($x < 4)

statementlist

echo $x is less than 4;

break;

else

case label :

print $x isnt less than 4;

statementlist
break;
...
default :
statementlist
}

one";

two";

three";
four";

another

You picked three You picked four

39

for ([initial expression]; [condition]; [update expression])


{
statement[s] inside loop
}

40

while (expression)
{
statements
}

V d:
$i = 1; $j = 9;
while ($i <= 10) {
$temp = $i * $j;
print $j * $i = $temp<br>";

do
{

$i++;

statements
}while (expression);

41

42

43

44

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>';

function functionName ([parameter1]...[,parameterN])


{
statement[s] ;
}
function functionName ([parameter1]...[,parameterN])
{
statement[s] ;
return .. ;
}

<?php
function doublevalue($var=5)
{
global $temp;
$temp = $var * 2;
}
$temp = 5;
doublevalue();
echo "\$temp is: $temp";
?>

45

<?php
function doublevalue(
{
$var = $var * 2;
}

$var)

46

// functions.inc

// index.php

<?php

<html>

function bold($string)

<head>
<title>Simple Function

{
echo "<b>" . $string .

$variable = 5;
doublevalue($variable);
echo "\$variable is: $variable";
?>

Call</title>

"</b>\n";

</head>

<body bgcolor="#ffffff">

?>

<?
include
"functions.inc";
require
"functions.inc";
bold("this is bold");
$myString = "this is bold";
bold($myString);
?>
</body></html>

47

48

class class_name() [extends superclass_name]


{
var $attribute;

function method_name()
{
$this->attribute = ;
}

$a = new class_name();

49

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;

12/9/2013

50

You might also like