ServerSide Scripting using PHP - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

ServerSide Scripting using PHP

Description:

Discuss how PHP can access MySQL databases. Background to PHP ... PHP & MySQL (1) ... http://www.mysql.com/ PHP Pocket Reference, Rasmus Lerdorf, O'Reilly, 2003, ... – PowerPoint PPT presentation

Number of Views:326
Avg rating:3.0/5.0
Slides: 25
Provided by: mnw
Category:

less

Transcript and Presenter's Notes

Title: ServerSide Scripting using PHP


1
Server-Side Scripting using PHP
  • Tools Methodologies 1
  • (F23HF1)
  • Mike Wicks
  • www.macs.hw.ac.uk/mnw1
  • EM 1.38

2
Objectives
  • After this Lecture you should be able to
  • Explain how Scripting with PHP works
  • Describe the basic PHP Syntax
  • Analyse how PHP can handle State via Sessions
  • Discuss how PHP can access MySQL databases

3
Background to PHP
  • Originally called Personal Home Page tools
  • Created by Rasmus Lerdorf, and released in June
    1995
  • Now more commonly known as the PHP Hypertext
    Preprocessor
  • Web-server is configured to identify HTTP
    requests that contain PHP
  • Interpreter processes the PHP Scripts, passes
    results back to Client
  • PHP is an HTML-embedded Server-Side scripting
    language
  • c/w Perl, a standalone Scripting Language
    separate Perl programs are created
  • c/w Javascript etc. Client-Side scripting
  • Syntax bits of C, Java and Perl with some
    unique PHP things thrown in.
  • The goal of the language is to allow web
    developers to write dynamically generated pages
    quickly.
  • http//www.php.net/

4
A Simple Webpage with some Embedded PHP
  • So what does some embedded PHP script look like?
  • lthtmlgt
  • ltheadgt
  • lttitlegtPHP Testlt/titlegt
  • lt/headgt
  • ltbodygt
  • lt?php
  • echo 'lth1gtHello Worldlt/h1gt' // Guess what
    this does?
  • ?gt
  • lt/bodygt
  • lt/htmlgt
  • Save this anywhere under YOUR personal webspace,
    as xyz.php
  • All the examples in this lecture can be found at
  • http//www.macs.hw.ac.uk/mnw1/php/........

5
Browsing the Webpage with Embedded PHP
  • Point your browser at (in my case)
  • http//www.macs.hw.ac.uk/mnw1/php/hello.php
  • And, hey presto!

6
Different ways to embed PHP
  • EITHER
  • lt?php
  • echo 'lth1gtHello Worldlt/h1gt'
  • ?gt
  • OR
  • ltscript language"php"gt
  • echo 'lth1gtHello Worldlt/h1gt'
  • lt/scriptgt
  • Are both legal ways to embed PHP statements in
    HTML pages

7
Variables (1)
  • Variables are defined with a dollar sign followed
    by the name of the variable
  • name Mike
  • ARE case-sensitive
  • name is NOT the same as Name
  • A valid variable name starts with a letter or
    underscore (not a number) followed by any number
    of letters, numbers, or underscores.
  • name Valid
  • namE Valid
  • _name Valid
  • _4name Valid
  • 4name Invalid!

8
Variables (2)
  • PHP Variables
  • do not have to be explicitly declared,
  • they are UNTYPED
  • meaning you can assign anything to your variable
  • PHP Variables can handle the following data
    types
  • boolean
  • integer
  • float (like a double)
  • string
  • array
  • resource
  • NULL
  • You can create a big mess in PHP, very quickly,
    if you are lazy or careless with your variables
    Read on!

9
Variable Variables(!)
  • A variable variable takes the value of a variable
    and treats that as the name of a variable, for
    example
  • lt?php
  • a 'hello'
  • a 'world'
  • echo "lth1gta alt/h1gt\n"
  • echo " lth1gta hellolt/h1gt"
  • ?gt
  • Assign to the Variable a the String hello
  • Assign to the Variable hello the String
    world
  • Write to the Browser the value of Variable a,
    followed by the value of the variable whose name
    is the value of Variable a.
  • In other words
  • Write to the browser the value of Variable a,
    followed by the value of the Variable hello.
  • http//www.macs.hw.ac.uk/mnw1/php/vvariables.php

10
Reference Variables
  • These are reminiscent of pointers in C, for
    example
  • lt?php
  • One 'Mike'
  • Two One
  • Two "My name is Two"
  • echo "lth1gtOnelt/h1gt\n"
  • echo " lth1gtTwolt/h1gt"
  • ?gt
  • Assign the value 'Mike' to Variable One
  • Reference Variable One via Variable Two
  • Alter Variable Two
  • Print out the Variable One
  • Print out the Variable Two
  • NB. Variable Two has been altered in the same
    way as Variable One!!!
  • http//www.macs.hw.ac.uk/mnw1/php/rvariables.php

11
Operators (1)
  • PHP has all the usual operators as part of its
    Syntax
  • Arithmetic Operators
  • Negate, Add, Subtract, Multiply, Divide, Modulus,
    Increment (Pre Post), Decrement (Pre Post)
  • -a, a b, a - b, a b, a / b, a b,
    a, a, --a, a-
  • Assignment Operators
  • a b, a - b, a b, a / b, a b

12
Operators (2)
  • Logical Operators
  • And (1), And (2), Or (1), Or (2), Exclusive Or,
    Not
  • a and b, a b, a or b, a b, a xor
    b, ! a
  • Comparison Operators
  • Equal, Identical (Equal and of the same Type),
    Not Equal (1), Not Equal (2), Not Identical (Not
    Equal or not of the same Type), Less Than,
    Greater Than, Less Than Or Equal To, Greater Than
    Or Equal To
  • a b, a b, a ! b, a ltgt b, a !
    b, a lt b, a gt b, a lt b, a gt b

13
Control Structures
  • Again, PHP has all the usual control mechanisms,
    stylistically influenced by C
  • If
  • While
  • Do-while
  • For
  • Foreach Interesting Array processing
  • Switch complicated IF, please look up!
  • Most control structures have 2 syntactically
    identical formats!

14
If
  • Format 1
  • if (a gt b)    echo "a is bigger than b"  
    b a
  • if (a gt b)    echo "a is bigger than b"
    else    echo "a is NOT bigger than b"
  • if (a gt b)    echo "a is bigger than b"
    elseif (a b)    echo "a is equal to b"
    else    echo "a is smaller than b"
  • I prefer this format, so this is what Im going
    to use from now on!
  • Format 2
  • if (a gt b)   echo "a is bigger than b"   b
    aendif
  • if (a gt b)   echo "a is bigger than
    b"else   echo "a is NOT bigger than
    b"endif
  • if (a gt b)   echo "a is bigger than
    b"elseif (a b)   echo "a is equal to
    b"else   echo "a is smaller than b"endif
  • I dont like this format!

15
While Do-while
  • Do
  • .
  • while ( condition )
  • Test the condition last
  • always do the loop at least once!
  • i 0
  • do
  • i
  • echo i
  • echo "ltbrgt"
  • while (i lt 10)
  • http//www.macs.hw.ac.uk/mnw1/php/dowhile.php
  • While ( condition )
  • .
  • Test the condition first
  • might not do the loop at all!
  • i 1
  • while (i lt 10)
  • echo i
  • echo "ltbrgt"
  • http//www.macs.hw.ac.uk/mnw1/php/while.php

16
For
  • For (initialise, condition, increment)
  • .
  • .
  • .
  • for (i 1 i lt 10 i)
  • echo i
  • echo "ltbrgt"
  • http//www.macs.hw.ac.uk/mnw1/php/for.php

17
Foreach
  • Iterate over Arrays
  • arr array("1", "2", "3", "4", "5", "6", "7",
    "8", "9", "10")
  • reset(arr) // Point at the 1st array item
  • foreach (arr as value)
  • echo "valueltbr /gt\n"
  • a array(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
  • i 0
  • foreach (a as v)
  • echo "\ai gt vltbrgt"
  • i
  • http//www.macs.hw.ac.uk/mnw1/php/foreach.php

18
Functions
  • PHP can handle sub routines, like most other
    languages
  • a array(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
  • i 0
  • foreach (a as v)
  • b double(v)
  • echo "\ai gt bltbrgt"
  • i
  • function double(a)
  • a a 2
  • return a
  • http//www.macs.hw.ac.uk/mnw1/php/function.php

19
Sessions (1)
  • Sessions are a feature of PHP that allows certain
    declared variables to persist across multiple
    web-pages.
  • lth1gtSession 1lt/h1gt
  • ltpgtRegister some variables.lt/pgt
  • lt?php
  • session_start()
  • session_register('var1')
  • session_register('var2')
  • var1 "Session"
  • var2 "Two"
  • ?gt
  • ltpgtTry lta href"session2.php"gtSession 2lt/agtlt/pgt
  • http//www.macs.hw.ac.uk/mnw1/php/session1.php

20
Sessions (2)
  • Clicking on the link in Session1.php takes us to
    the following page
  • lth1gtSession 2lt/h1gt
  • ltpgtWhat is in the Registered variables, and then
    unregister them.lt/pgt
  • lt?php
  • session_start()
  • echo "\var1 _SESSIONvar1ltbrgt"
  • echo "\var2 _SESSIONvar2ltbrgt"
  • session_unregister(var1)
  • session_unregister(var2)
  • ?gt
  • ltpgtTry a Refresh?lt/pgt
  • ltpgtTry lta href"session1.php"gtSession 1lt/agt
    againlt/pgt
  • http//www.macs.hw.ac.uk/mnw1/php/session2.php
  • The Sessions feature of PHP allows application
    designers to easily build systems that are rich,
    flexible and functional

21
PHP MySQL (1)
  • Rich N-Tier Applications can be built using PHP
    scripts that access databases, combined with PHP
    Session Handling facilities
  • MySQL an Open Source DB
  • http//www.mysql.com/
  • PHP Function Calls
  • Install the MySQL Extension to the PHP
    Interpreter in the Web-Server
  • mysql_pconnect()
  • connects to a database
  • mysql_db_query()
  • passes SQL to the database
  • mysql_fetch_row()
  • retrieves rows from result sets created by an SQL
    query
  • mysql_free_result()
  • discards a result set
  • Simple Example (no sessions!)
  • http//www.macs.hw.ac.uk/mnw1/php/baby.php
  • PHP Interface to a Database of Baby Names
  • User can Suggest new names

22
PHP MySQL (2)
  • MySQL Functions used
  • mysql_pconnect("brahma",,)
  • mysql_db_query(db,"insert into table values
    ('new_name',0)")
  • mysql_db_query(db,"update table set
    votesvotes1 where name'vote'")
  • resultmysql_db_query(db,"select sum(votes) as
    sum from table")
  • mysql_free_result(result)
  • resultmysql_db_query(db,"select from table
    order by votes DESC")
  • rowmysql_fetch_row(result)
  • echo row0
  • echo row1
  • mysql_free_result(result)

23
PHP MySQL (3)
1
2
3
Example Open Source application that uses PHP and
MySQL http//www.phpsurveyor.org/ My
Work http//www.macs.hw.ac.uk/mnw1/phpsurveyor/i
ndex.php?sid1
24
Summary
  • Background
  • Embedding PHP into HTML pages
  • Language (Syntax)
  • Variables
  • Operators
  • Control Structures
  • Functions
  • Objects are FULLY supported in PHP
  • Sessions
  • MySQL Access
  • Acknowledgments
  • http//www.php.net
  • http//www.mysql.com/
  • PHP Pocket Reference, Rasmus Lerdorf, OReilly,
    2003, ISBN 0-596-00402-8
  • http//www.oreilly.com/catalog/phppr/chapter/php_
    pkt.html
Write a Comment
User Comments (0)
About PowerShow.com