ch.6b php basic

Upload: wfscmgbh

Post on 30-May-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 ch.6b php basic

    1/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 and e-Business ManagementConcentration

    Department of Finance and Decision Sciences

    School of Business

    Hong Kong Baptist University

    PHP Basic

    < tm > < tm > are used to inform the web server this is a html program

    Just send to client browser to interpret

    e ex ens on Filename.htm or Filename.html

    First Tag

    Second Tag

    =

    The web server will parsed it using the PHP parser which is the Zend engine in oursystem

    File extension Filename.php or Filename.php3 or Filename.php4

    First example phptags.php http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/phptags.php

  • 8/9/2019 ch.6b php basic

    2/12

    Hong Kong Baptist University

    PHP Basic

    Second Example

    Combining HTML and PHPfirstscri t. h

    http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/firstscript.php

    Both tab and PHP tag exist in the same programw t extens on .p p

    Program note Command (Echo)

    The String (

    Hello World! Im using PHP!

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

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

    Fourth Examples with error in escape (\) charactererrorscript2-a.php

    ttp: sem . u.e u. ~ semuser p pexamp es errorscr pt -a.p p

    errorscript2-b.phphtt ://isem03.hkbu.edu.hk/~isemuser20/ h exam les/errorscri t2-b. h

  • 8/9/2019 ch.6b php basic

    3/12

    Hong Kong Baptist University

    PHP Basic

    Commenting your code Insert comments into code

    //single line comment

    # another single line comment

    /*

    multiple

    line

    comment

    */

    Fifth Example comments. h

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

    Hong Kong Baptist University

    Variable

    , Tony or Cool or 100 or

    Example

    $username = Tony; $mark= 100;

    Use meaningful name such as

    $totalmark, $studentname, $brandofcar or

    Name you need to guess $p, $benz, $honda,

  • 8/9/2019 ch.6b php basic

    4/12

    Hong Kong Baptist University

    Variables and Operators in PHP

    Data Type in PHP Standard Type

    Integer

    Double

    String (USING or TO quote the character andusin \ and\ to rint and within the strin

    Boolean

    Object

    Array

    Special Data Type Resource (Reference to a third party resources)

    NULL (An uninitialized variables)

    Sixth example printvarscript.php

    . . . .

    Hong Kong Baptist University

    -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

    _

    Contains variables about server environment

    SESSION_ Contains variables registered in a session

  • 8/9/2019 ch.6b php basic

    5/12

    Hong Kong Baptist University

    Constants

    A constant is an identifier for a value that cannot

    chan e durin the course of a scri t

    _ ,

    E.g. define (TONY_CONST, 114);

    Remark

    in definition)

    E.g. define (TONY_CONST, 114 , true);

    Then Tony_Const = TONY_CONST =

    Hong Kong Baptist University

    Constants

    Seventh example constants.php

    p: sem . u.e u. ~ semuser p pexamp es cons an s.p p

    Some redefined constants _FILE_ ; _LINE_ ; PHP_VERSION ; PHP_OS

    ghth examp e constants2.php

    ~. . . .

  • 8/9/2019 ch.6b php basic

    6/12

    Hong Kong Baptist University

    Assignment and Arithmetic operators

    ss gnmen =

    Arithmetic on

    Substraction -

    Multi lication *

    Division /

    Modulus %

    Hong Kong Baptist University

    operators

    Concatenation .

    E.g. hello . world return helloworld Combined Assignment

    += , -= , *= ,/= , %= , .=

    E.g. $x *= 5 is equivalent to $x = $x * 5;

  • 8/9/2019 ch.6b php basic

    7/12

    Hong Kong Baptist University

    Comparsion Operators

    == equ va en

    != non-equivalent === ent ca same ata type

    >

    >=

    == === !=

    &&

    ||

    * - .

    and

    xor

    or

  • 8/9/2019 ch.6b php basic

    9/12

    Hong Kong Baptist University

    Operators examples

    ass gnscr p .p p

    http://isem03.hkbu.edu.hk/~isemuser20/phpexamples/assignscript.php

    arithmeticscript.php

    htt ://isem03.hkbu.edu.hk/~isemuser20/ h exam les/arithmeticscri t. h

    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

    s ng se ype settype ( variable_name, data_type);

    . . ,

    E.g.

    $tonydata = 100.00;

    $newdata = (string) $tonydata;

    variable

  • 8/9/2019 ch.6b php basic

    10/12

    Hong Kong Baptist University

    Flow Control - ifStatement

    {

    // 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

    {

    case result1 :

    // excute if expression results in results 1break;

    // excute if expression results in results 2

    break;

    default :

    // execute this if no break statement

    // has been encountered hitherto

    }

  • 8/9/2019 ch.6b php basic

    11/12

    Hong Kong Baptist University

    ? Operator

    (expression)?returned_if_expression_is_true:returned_if_expression_ s_ a se;

    Hong Kong Baptist University

    Flow control - while, do while

    {

    }

    do

    {

    // code to be excuted

    }

    while (expression);

  • 8/9/2019 ch.6b php basic

    12/12

    Hong Kong Baptist University

    or a emen

    for (variable assignment; test expression; variable increment)

    {

    }

    using continue to skip the iteration

    For ($a=0; $a