Beruflich Dokumente
Kultur Dokumente
Week 6
22/02/2010
PHP Basic
<html>
ht l </html>
/ht l
are used to inform the web server this is a html program
Just send to client browser to interpret
Fil extension
File t i
Filename.htm or Filename.html
First example
p
phptags.php http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/phptags.php
Hong Kong Baptist University
PHP Basic
Second Example
Combining HTML and PHP
firstscript.php
p p p
http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/firstscript.php
Both <html> </html> tab and PHP tag <?php ?> exist in the same program
with
i h extension
i .php
h
Program note
C
Command d (Echo)
(E h )
The String (<p><em>Hello World! I’m using PHP!</em></p>), and
The instruction terminator (;)
PHP Basic
errorscript-b.php
• http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/errorscript-b.php
errorscript2-b.php
http://isem03.hkbu.edu.hk/ isemuser20/phpexamples/errorscript2 b.php
http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/errorscript2-b.php
Hong Kong Baptist University
PHP Basic
Fifth Example
comments.php
• http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/comments.php
Variable
Representing a particular value,
value such as
Tony or Cool or 100 or …
Example
$username = “Tony”;
$mark = 100;
Sixth example
printvarscript.php
http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/printvarscript.php
http://isem03 hkbu edu hk/~isemuser20/phpexamples/printvarscript php
Pre-Defined Variables
http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/phpinfo.php
$_GET
Contains variables with get method
$_POST
Contains variables with put method
$_COOKIE
Contains variables through cookies
$_FILES
Contains variables through file upload
$ ENV
$_ENV
Contains variables about server environment
$ SESSION
$_SESSION
Contains variables registered in a session
Hong Kong Baptist University
Constants
define ( “CONSTANT
CONSTANT_NAME
NAME”, value);
E.g. define (“TONY_CONST”, 114);
Remark
(make Constant name case insensitive by adding the true clause
in definition)
E.g. define (“TONY_CONST”, 114 , true);
Constants
Seventh example
constants.php
http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/constants.php
http://isem03 hkb ed hk/ isem ser20/phpe amples/constants php
Ei h h example
Eighth l
constants2.php
http://isem03.hkbu.edu.hk/
http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/constants2.php
isemuser20/phpexamples/constants2.php
Hong Kong Baptist University
Combined Assignment
+= , -= , *= , /= , %= , .=
E.g. $x *= 5 is equivalent to $x = $x * 5;
Hong Kong Baptist University
Comparsion Operators
== equivalent
i l t
!= non-equivalent
=== id ti l (same
identical ( data
d t type)
t )
>
>=
<
<=
Logical Operators
|| , or
Xor left or right is true but not both
&& , andd
! not
Hong Kong Baptist University
Operators examples
assignscript.php
i i t h
http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/assignscript.php
arithmeticscript.php
http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/arithmeticscript.php
p p p p p p p
comparisonscript.php
http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/comparisonscript.php
logicalscript.php
http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/logicalscript.php
Using Casting
E.g.
$tonydata = 100.00;
$newdata = (string) $tonydata;
? Operator
(expression)?returned_if_expression_is_true:returned_if_expressio
n_is_false;
i f l
do
{
// code to be excuted
}
while (expression);
Hong Kong Baptist University
f Statement
for St t t
for (variable assignment; test expression; variable increment)
{
// code to be executed
}
T PHP L
Try Laboratory
b t A
Activity
ti it now !!,
!!
ready
d by
b 01/03/2010 11:00a.m.
11 00 iin your account