: Edwin - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

: Edwin

Description:

??????. ??: Edwin. ??: kcchu6_at_cse.cuhk.edu.hk. MSN: ed_at_edwinchu.info. ICQ: 46374052. ????: 98340556 ... Language developed by Rasmus Lerdorf from the Apache Group ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 16
Provided by: edwi3
Category:
Tags: edwin | icq

less

Transcript and Presenter's Notes

Title: : Edwin


1
??????
  • ?? Edwin
  • ?? kcchu6_at_cse.cuhk.edu.hkMSN
    ed_at_edwinchu.infoICQ 46374052???? 98340556
  • ???? http//edwincheese.schtuff.com/interactive_w
    eb_programming

2
??????
  • ???
  • PHP ??(????)??????????

3
Intro to PHP
  • What is PHP?
  • Language developed by Rasmus Lerdorf from the
    Apache Group
  • Its primary use is for server-side scripting
  • Ex To process HTML forms
  • PHP scripts are typically embedded within html
    documents
  • The server processes the html document, executing
    the PHP segments and substituting the output
    within the html document

4
Intro to PHP
  • The following .php file produces the html shown
  • lthtmlgt
  • ltheadgt
  • lttitlegtFirst PHP Examplelt/titlegt
  • lt/headgt
  • ltbodygt
  • lt?php echo "ltpgtlth1gtOutputlt/h1gt"
  • echo "lth2gtOutputlt/h2gt"
  • echo "lth3gtOutputlt/h3gtlt/pgt"
  • ?gt
  • ltscript language"PHP"gt
  • echo "\nltbgtMore PHP Outputlt/bgtltbr /gt\n"
  • lt/scriptgt
  • lt/bodygt
  • lt/htmlgt
  • --------------------------------------------------
    --------------
  • lthtmlgt
  • ltheadgt
  • lttitlegtFirst PHP Examplelt/titlegt

5
Intro to PHP
  • Simple (scalar) types
  • boolean
  • TRUE or FALSE
  • integer
  • Platform dependent size of one machine word
  • 32 bits on most machines
  • float
  • Double precision
  • We could call it a double, but since we don't
    declare variables (we will discuss shortly) float
    works

6
Intro to PHP
  • string
  • We have single-quoted and double-quoted string
    literals
  • Double quoted allows for more escape sequences
    and allows variables to be interpolated into the
    string
  • What does that mean?
  • Rather than outputting the name of the variable,
    we output its contents, even within a quote
  • We'll see an example once we define variables
  • Note that this is NOT done in Java
  • Length can be arbitrary
  • Grows as necessary

7
Intro to PHP
  • PHP variables
  • All PHP variables begin with the
  • Variable names can begin with an underscore
  • Otherwise rules are similar to most other
    languages
  • Variables are dynamically typed
  • No type declarations
  • Variables are BOUND or UNBOUND
  • Unbound variables have the value NULL
  • Type information is obtained from the current
    bound value

8
Intro to PHP
  • PHP programs have access to a large number of
    predefined variables
  • These variables allow the script access to server
    information, form parameters, environment
    information, etc.
  • Ex
  • _SERVER is an array containing much information
    about the server
  • _POST is an array containing variables passed to
    a script via HTTP POST
  • _COOKIE is an array containing cookies
  • See ex4.php

9
Intro to PHP
  • PHP Expressions and Operators
  • Similar to those in C / Java / Perl
  • Be careful with a few operators
  • / in PHP is always floating point division
  • To get integer division, we must cast to int
  • x 15
  • y 6
  • echo (x/y), (int) (x/y), "ltBR /gt"
  • Output is 2.5 2
  • Inequality operators do not compare strings
  • Will cast strings into numbers before comparing

10
Intro to PHP
  • To compare strings, use the C-like string
    comparison function, strcmp()
  • Other string functions are listed on p. 482
  • Even the operator has odd behavior in PHP when
    operands are mixed
  • Ex Consider comparing a string and an int
  • Any non-numeric PHP string value will equal 0
  • Any numeric PHP string will equal the number it
    represents
  • Ex Consider comparing a string and a boolean
  • Regular PHP string value will equal true
  • 0 string will equal false
  • This behavior is consistent but confusing to the
    programmer and is probably best avoided

11
Intro to PHP
  • An additional equality operator and inequality
    operator are defined
  • returns true only if the variables have the
    same value and are of the same type
  • If casting occurred to compare, the result is
    false
  • ! returns true if the operands differ in value
    or in type
  • Precedence and associativity are similar to
    C/Java
  • See Table 11-1 in the online PHP manual

12
Intro to PHP
  • PHP Control Structures
  • Again, these are similar to those in C / Java
  • if, while, do, for, switch are virtually
    identical to those in C and Java
  • PHP allows for an alternative syntax to designate
    a block in the if, while, for and switch
    statements
  • Open the block with rather than
  • Close the block with endif, endwhile, endfor,
    endswitch
  • Advantage to this syntax is readability
  • Now instead of seeing a number of close braces,
    we see different keywords to close different
    types of control structures

13
Intro to PHP
  • A nice feature of PHP is that the "control"
    resulting from a control structure is maintained
    even when you exit back to html mode
  • Thus, in lt?php you can branch / loop etc.
  • You can then exit php ?gt and format in straight
    html
  • PHP also has the foreach loop
  • We will look at this when we discuss arrays
  • See ex5.php

14
Processing Forms with PHP
  • Using PHP with forms fairly simple
  • When forms are submitted the server executes the
    php script, returning the resulting html
  • Remember that some of the file is unchanged,
    since it may not have an embedded php script
    within it
  • Server can be set to that the form variables can
    be accessed directly by simply using the sign
  • However, it is better to access the variables
    from the _POST array (or the _GET array)
  • The form element name is the key into the array
  • Discuss

15
Processing Forms with PHP
  • We can also use PHP to create forms
  • However, it is really just HTML that we are using
  • We can "interleave" the PHP and html to get the
    desired overall result
  • So if you don't know it yet learn some HTML
Write a Comment
User Comments (0)
About PowerShow.com