Sie sind auf Seite 1von 12

Hong Kong Baptist University

Week 6

Basic PHP and Program Flow

22/02/2010

Dr. Tony Chun-Kuen WONG


Coordinator, BBA(Hons) Information Systems Management Major
Coordinator,, BBA(Hons)
( ) Information Systems
y and e-Business Management
g
Concentration
Department of Finance and Decision Sciences
School of Business

Hong Kong Baptist University

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

 Basic PHP Start and End Tags


 First Tag <?php ?>
 Second Tag <? ?>
 Third Tag <script language = “php”>
php > </script>
 The web server will parsed it using the PHP parser which is the Zend engine in our
system
 File extension
 Filename.php or Filename.php3 or Filename.php4

 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 (;)

Hong Kong Baptist University

PHP Basic

 Third Examples with error in instruction terminator


 errorscript-a.php
• http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/errorscript-a.php
p p p p p p p

 errorscript-b.php
• http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/errorscript-b.php

 Fourth Examples with error in escape (\) character


errorscript2-a.php
htt //i 03 hkb d hk/ i 20/ h l / i t2 h
http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/errorscript2-a.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

 Commenting your code


 Insert comments into code
 // single line comment
 # another single
g line comment
 /*
 multiple
 line
 comment
 */

 Fifth Example
 comments.php
• http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/comments.php

Hong Kong Baptist University

Variable
 Representing a particular value,
value such as
 Tony or Cool or 100 or …
 Example
 $username = “Tony”;

 $mark = 100;

 Naming the variable


 Use meaningful name such as
• $totalmark, $studentname, $brandofcar or
• Name you need to guess $p, $benz, $honda, …
Hong Kong Baptist University

Variables and Operators in PHP


 Data Type in PHP
 Standard Type
 Integer
 Double
 String (USING ‘ or “ TO quote the character and
using
g \ ‘ and \ “ to p
print ‘ and “ within the string)
g)
 Boolean
 Object
 Array
 S i l Data
Special D Type
T
 Resource (Reference to a third party resources)
 NULL (An uninitialized variables)

 Sixth example
 printvarscript.php
 http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/printvarscript.php
http://isem03 hkbu edu hk/~isemuser20/phpexamples/printvarscript php

Hong Kong Baptist University

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

 A constant is an identifier for a value that cannot


change
g during g the course of a script
p

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

 Then Tony_Const = TONY_CONST = …

Hong Kong Baptist University

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

 Some ppredefined constants


 _FILE_ ; _LINE_ ; PHP_VERSION ; PHP_OS

 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

Assignment and Arithmetic operators


 Assignment
A i t =
 Arithmetic
 Addition +
 Substraction -
 Multiplication
p *
 Division /
 Modulus %

Hong Kong Baptist University


Concatenation and Combined Assignment
operators
 Concatenation .
 E.g. ‘hello’ . ‘world’ return ‘helloworld’

 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 )
 >
 >=
 <
 <=

Hong Kong Baptist University

Logical Operators
 || , or
 Xor left or right is true but not both
 && , andd
 ! not
Hong Kong Baptist University

Auto increment and decrement of integer variable


 U i ++ andd --
Using
 E.g. $x++; is equivalent to $x = $x + 1;

 What will happen to the following code


(A) (B)
$x = 3; $x = 3;
$x++ < 4; ++$x < 4;

Hong Kong Baptist University

Order of precedence of selecting operators


++ -- (Cast)
(C )
/ * %
+ -
< <= => >
== === !=
&&
||
= += -= *== /= %= .=
=
and
xor
or
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

Hong Kong Baptist University

Changing Type in PHP


 Using settype ( )
 settype ( variable_name, ‘data_type’);
 E g settype ($tonydata , ‘boolean’);
E.g. boolean );

 Using Casting
 E.g.
$tonydata = 100.00;
$newdata = (string) $tonydata;

 Using gettype ( ) to obtain the data type of the


variable
Hong Kong Baptist University

Flow Control - if Statement


If (expression)
{
// code to excute if the expression evaluates to true
}
elseif (expression)
{
// code to excute if the previous expression failed and
// this one evaluates to true
}
else
{
// code to excute in all other cases
}

Hong Kong Baptist University

Flow control - switch Statement


switch (expression)
{
case result1 :
// excute if expression results in results 1
break;
case result2 :
// excute if expression results in results 2
break;
default :
// execute this if no break statement
// has been encountered hitherto
}
Hong Kong Baptist University

? Operator

(expression)?returned_if_expression_is_true:returned_if_expressio
n_is_false;
i f l

Hong Kong Baptist University

Flow control - while, do while


while (expression)
{
// do something
}

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
}

using break to escape from the Loop

using continue to skip the iteration

For ($a=0; $a <=3; $a++)


{
echo “a equals $a<br>”;
}

Hong Kong Baptist University

T PHP L
Try Laboratory
b t A
Activity
ti it now !!,
!!

with your work

ready
d by
b 01/03/2010 11:00a.m.
11 00 iin your account

Das könnte Ihnen auch gefallen