Web06_PHPBasic
Web06_PHPBasic
1. Introducing PHP
2. PHP Operation & Installation
3. Syntax
4. Out
5. Operator
6. Chain
7. Arrays
8. Controller structure
9. Jaw
HienLTH – KhietLTN Web Design 3
1. Introduction PHP – History
• PHP : Rasmus Lerdorf in 1994 (developed to generate login forms
using Unix HTTP protocol)
• PHP 2 (1995) Switch to the script language processing on the server.
Database support, File Upload, variable declaration, array, recursive
function, conditional sentence, expression, ...
• PHP 3 (1998) : Supports ODBC, multi-OS, email protocol (SNMP,
IMAP), PHP code parser (parser) by Zeev Suraski and Andi Gutmans
• PHP 4 (2000) : Become a standalone component for webserver. Parse
changed its name to Zend Engine. Additional security features for PHP
• PHP 5 (2005) : Add Zend Engine II to support OO, XML, SOAP
programming for Web Services, SQLite
• Phiên bản mới nhất của PHP là version PHP 7.3 (www.php.net –
12.02.2019)
(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
PHƯƠNG ÁN 2
http://www.wampserver.com/en/
• LAMP – Linux
• WAMP – Windows
• XAMPP – Windows/Linux
• Speech :
• Use <? Php…?> To highlight PHP commands
• The php statements are separated by ‘;’
• Irrespective space, tab or newline character.
• For example:
<?php echo “Hello”; echo “World!”; ?>
<?php
echo “Hello” ;
echo “World!”;
?>
HienLTH – KhietLTN Web Design 24
3. Syntax (tt) – Note
• For example
$var = “test”;
if (isset($var))
echo “Variable is Set”;
if (empty($var))
echo “Variable is Empty”;
Hàm str_pad
. [] ()
Toán học + - * / % ++ --
Luận lý && || ?: ,
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (PHP 4)
$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4)
36
$a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b.
HienLTH – KhietLTN Web Design 36
6. String
• Character Escape : \
– Use to write special characters in string
– EG:
Cần có chuỗi: Người ta nói “PHP rất tốt”
$a = “Người ta nói “PHP rất tốt””; //Sai
$a = “Người ta nói \“PHP rất tốt\””;
//Đúng
– Some characters must use an escape character : $, \, “
– Besides:
• \ n: Line break
• \ r: return to the beginning of the line
• \ t: tab
• ...
HienLTH – KhietLTN Web Design 40
6. String– document type
(heredoc)
– Allows writing 1 string on multiple lines
– No need to use the escape character:
– How to write:
$variable = <<<symbol
content on multiple lines
symbol;
– Note: Symbol must be written on the first
character of the line
– For example:
$a = <<<EOD
This is a multi-line string using the
'heredoc' document type syntax.
EOD;
HienLTH – KhietLTN Web Design 41
6. String– document type
(heredoc)
<?php
$str = <<<EOA
Example of string <br>
spanning multiple lines<br>
using heredoc syntax.<br>
EOA;//không được có khoảng trắng đầu dòng
echo($str);
$name = “mr bean";
$d = date("d/m/y");
$str = <<<EOQ
This is a lecture of $name.<br>
Ngay $d<br>
EOQ;
echo($str);
?>– KhietLTN
HienLTH Web Design 42
6. String – Handler function
• Search
– strpos($chuỗi, $chuỗi_con, $vị_trí_bắt đầu)
– strrpos ($chuỗi, $chuỗi_con, $vị_trí_bắt đầu)
• Compare
– strcmp($chuỗi_1, $chuỗi_2)
– strncmp($chuỗi_1, $chuỗi_2, $chiều_dài)
– strcasecmp($chuỗi_1, $chuỗi_2)
– strncasecmp($chuỗi_1, $chuỗi_2, $chiều_dài)
– strnatcmp($chuỗi_1, $chuỗi_2)
– strnatcasecmp($chuỗi_1, $chuỗi_2)
HienLTH – KhietLTN Web Design 44
6. String
<?php
?>
<?php
Xử lý chuỗi
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user,$pass,$uid,$gid,$gecos,$home,$shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?>
<?php
// PHP 5
foreach ($colors as &$color) {
$color = strtoupper($color);
}
unset($color); /* ensure that following writes to
$color will not modify the last array element */
Theo khóa
– ksort($mảng) // tăng dần
– krsort($mảng) // giảm dần
– uksort($mảng, ”hàm_so_sánh”) // tự định nghĩa thứ tự
• Search
array_search($value, $array)
➢ one-dimensional array:
1. Build an array import / export function
2. Write a function to find the max, min element in the array
3. Sort array ascending, descending
4. Finds the element with the x value in the array
5. Updates the value for the element with the x value in the array
6. Remove the element with x value in the array
➢ two-dimensional array:
1. Build an array import / export function
2. Export elements on the primary diagonal and secondary
diagonal.
3. Arrange the array descending clockwise
01 02 … 19 20
55
HienLTH – KhietLTN Web Design 55
Question 1: Which of the following
PHP lines of code is not valid?
A. $_10
B. ${“MyVar”}
C. &$something
D. $10_somethings
• Điều kiện if
• Điều khiển switch
• Vòng lặp for Tương tự như C++
• Vòng lặp while
• Vòng lặp do.. While
• Vòng lặp foreach
• Từ khóa break, continue
if (conditional expression)
command block 1
else
command block 2
switch (expressions)
{
case expressions 1:
command block 1
case expressions 2:
command block 2
...
case expressions n:
command block n
default:
command block end
}
HienLTH – KhietLTN Web Design 79
8.2 The switch statement
switch ($a) {
case 0:
echo "a = 0";
break;
case 1:
echo "a = 1";
break;
}
HienLTH – KhietLTN Web Design 80
8.3 While / do ... while loop
For example:
$i = 1; $j = 9;
$temp = $i * $j;
print “$j * $i =
$temp<br>";
$i++;
▪ For example:
print “<select>”;
for ($i = 1; $i <= 12; $i++)
{
print
“<option>$i</option>”;
}
print “</select>”;
For example:
• break
Stop and exit the current loop
• continue
Stop performing the current iteration to go to
the next iteration
<?php
$arr = array("one", "two", "three");
reset($arr);// reset pointer, start again on first element
while (list(, $value) = each($arr)) {
echo "Value: $value<br />\n";
}
$input = array(4,7);
takes_array($input);
?>
<?php
Note: dấu &
function add_some_extra(&$string)
{
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str;// outputs 'This is a string, and something extra.'
?>
• Parameter transmission
By default, parameters are passed inside the function by the
method of value. If you want to change directly on the
passed parameters, you use the reference method, add &
before the parameter name (when defining) as well as the
variable name passed as a parameter (when calling the
function).
HienLTH – KhietLTN Web Design 93
9. Function
• Static variable
Add static keyword when declaring variables
Initialized (and assigning values) a single first time during the
execution of the script
• Use global variables
Declare global variables with the keyword global (inside the
function) to be able to use this global variable inside the function.
Use the following functions to get the list of parameters:
func_num_args (): number of parameters when the function is
called
func_get_arg (i): value of the ith parameters passed (starting at 0)
func_get_args (): list of all parameters
• Scope
Valid for use throughout script, even before and
after definition
• Nested functions
Allows defining function nested, even nested
inside a control structure (if, switch, while / do,
while ...)
This type of function is scoped throughout the
script and cannot be redefined
<?php
function small_numbers()
{
return array (0, 1, 2);
}
list($zero,$one,$two)=small_numbers();
?>
$func = 'bar';
$func('test'); // This calls bar()
$func = 'echoit';
$func('test'); // This calls echoit()
?>
function Bar()
{
echo "This is Bar";
}
}
$foo = new Foo();
$funcname = "Variable";
$foo->$funcname(); // This calls $foo->Variable()
?>
$GLOBALS contains all the global variables that the local script can access
HienLTH – KhietLTN
? Web Design 105
Reference
• Website W3school
• Slide lập trình Web, ĐH KHTN, 2007
▪ Họ tên:
▪ Mã SV:
▪ Lớp:
▪ Khoá:
▪ Email:
01 02 … 19 20