PHP I - PowerPoint PPT Presentation

About This Presentation
Title:

PHP I

Description:

Originally created in 1994 by Rasmus Lerdorf, to track users at his web site. ... p Kevin's birthday is ?php echo('$birthdays[ Kevin']'); ? . would result in p ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 21
Provided by: femi6
Category:

less

Transcript and Presenter's Notes

Title: PHP I


1
PHP I
2
  • PHP, or PHP Hypertext Preprocessor is a
    server-side scripting language.
  • Originally created in 1994 by Rasmus Lerdorf, to
    track users at his web site.
  • Over the last few years, it has rapidly developed
    into a server-side scripting language, that fully
    supports (interacts) with a host of relational
    database systems (MySql in particular), thereby
    making it extremely useful for generating dynamic
    page content.
  • Latest version is version 4, but 5 due to be
    released soon.
  • In addition, PHP is an open source technology,
    that is freely available. It is estimated that
    over 6 million domains now use PHP.
  • It is platform independent, with implementations
    for most Unix, Linux and Windows operating
    systems.

3
  • Shares some similarity with JavaScript in that it
    also allows you to embed programs (scripts)
    inside HTML code.
  • The distinction is that JavaScript (and other
    client-side scripting languages) are interpreted
    by the browser, whilst PHP (and other server-side
    scripting languages) is processed by the web
    server.
  • Once the script is processed, the result of the
    script (HTML in most cases) replaces the PHP
    code, and is returned to the client.
  • Hence the browser sees standard HTML and displays
    it appropriately.
  • As the script is processed by the server, the
    technology is aptly named server-side scripting
    language

4
  • Sample PHP code
  • lthtmlgtltheadgtlttitlegtWelcome to the world of
    PHPlt/titlegtlt/headgt
  • ltbodygt
  • ltpgt
  • lt?php
  • echo(Welcome to the world of PHP, a convenient
    means of generating dynamic page content.)
  • ?gt
  • lt/pgt
  • lt/bodygt
  • lt/htmlgt
  • Resultant HTML code
  • lthtmlgtltheadgtlttitlegtWelcome to the world of
    PHPlt/titlegtlt/headgt
  • ltbodygt
  • ltpgt Welcome to the world of PHP, a convenient
    means of generating dynamic page content.lt/pgt
  • lt/bodygt lt/htmlgt

5
  • Advantages of Server-side scripting
  • There are no browser incompatibility issues, as
    the PHP code is interpreted entirely by the
    server.
  • Access to server side resources such as data from
    a relational database. This is perhaps the most
    important benefit of PHP.
  • Reduced load on the client JavaScript may
    significantly slow the display of a web page on
    low memory computers, as the browser has to
    process the JavaScript code. PHP code is entirely
    processed by the web server, hence this burden is
    passed to the server.
  • Drawbacks of Server-side scripting
  • Multiple server calls As PHP code is interpreted
    by the web server, each time a user interacts
    with the page, a call has to be made to the web
    server.
  • Security issues Use of PHP could expose the web
    server, and lead to unwanted access to web server
    resources.

6
  • Basic Syntax
  • PHP scripts begin with lt?PHP and end with ?gt
    delimiters
  • Any code within these 2 delimiters is processed
    by the web server as PHP code. PHP code can be
    embedded directly anywhere within an HTML
    document.
  • PHP files are stored with a .php extension.
  • Syntactically, PHP is very similar to JavaScript,
    and supports similar constructs such as Arrays
    and Conditional statements.
  • In addition, PHP comes with a lot of built-in
    functions, that enable you to perform all sorts
    of complex tasks such as sending emails, file
    upload, downloads, etc.
  • Single line comments begin with //.
  • Multiple line comments are enclosed between /
    and /
  • //this is a single line comment.
  • / this is a multi-line
  • comment /

7
  • Variables and Operators
  • Variables in PHP are declared with symbol.
  • PHP is a loosely typed language, i.e. its
    variables can contain any type of data such as
    integers, strings, etc. They may also change
    types over its lifetime.
  • testvariable Hello World
  • testvariable 3
  • In the above example, testvariable is initially
    initialized with a string value, and subsequently
    assigned an integer value.
  • You may use the settype built-in function to
    explicitly convert the type. E.g.
    settype(testvariable, integer) converts the
    type of testvariable from string, to integer.
  • The is the assignment operator, while is
    the equality operator.
  • Several comparative, and arithmetic operators
    (similar) to those in JavaScript exist for
    performing comparisons, and for computations.

8
  • PHP automatically converts variable types when
    used for arithmetic computation. E.g
  • lt?PHP
  • testvariable20 students
  • testvariabletestvariable 20.
  • The above statement would evaluate to 40.
  • Note that for implicit conversion from string to
    integer, the number value must come before other
    text in the string.
  • Variable value substitution
  • Variable values can be directly written into HTML
    code, e.g
  • lthtmlgtltheadgtlttitlegtPHP Examplelt/titlegtlt/headgt
  • ltbodygt
  • ltpgt Hello
  • lt?php testvariablePaul Smith
    echo(testvariable)
  • lt/pgt
  • lt/bodygt
  • lt/htmlgt

9
  • Array Declaration
  • In the simplest form, arrays are declared using
    the built-in array function, as shown below
  • anarray array (1,ten,four)
  • The above code creates an array called anarray,
    that has 3 values.
  • As in JavaScript and other similar programming
    languages, array indices start at 0, i.e.
  • anarray0 1, anarray1 ten and
    anarray2 four
  • It is also possible to assign values to array
    elements with the index. E.g. anotherarray0
    ten
  • If the index is not specified, an array element
    will be added after the last existing element of
    the array, i.e.
  • anarray the fourth element.
  • The above code automatically creates the 4th
    element of anarray.

10
  • PHP also supports the use of non-numeric arrays
    (associative arrays).
  • E.g.
  • birthdaysKevin 20-03-1982
  • birthdaysJames 21-07-1984
  • Associative arrays are particularly useful for
    accessing, and processing HTML form elements, as
    demonstrated later.
  • Accessing values of array elements
  • The array followed by its index (in square
    brackets) translates to the value of the element.
  • Illustrating with the examples above,
  • ltpgtlt?php echo(anarray1) ?gt would result
    in ltpgttenlt/pgt
  • ltpgtKevins birthday is lt?php
    echo(birthdaysKevin) ?gt. would result in
    ltpgtKevins birthday is 20-03-1982.

11
  • User Interaction and Forms
  • Server-side languages such as PHP are limited in
    their ability to interact with user input in the
    sense that a request has to be sent to the
    server, server processes the PHP code to
    dynamically generate an HTML response, and sends
    this back to the client.
  • Hence user interaction involves back and forth
    server calls.
  • Sending information to the web server
  • The simplest method of sending information from a
    client, to the web server is by sending it along
    with the URL string as a query string.
  • This is accomplished with the use of a ? symbol
    before the variable that is being passed. In the
    case of sending multiple variables, they are
    separated with an ampersand ()
  • E.g lta hrefphp_script.php?firstnamePaulgtPaullt/
    agt

12
  • The previous example demonstrates a link to a PHP
    program called php_script.php.
  • The name variable is passed to the script in URL
    of the link, as part of the page request.
  • PHP automatically creates an associative array
    variable called _GET that contains any values
    passed in the URL query string.
  • Hence the value of the name variable passed in
    the example would be accessed with
    _GETfirstname.
  • Example
  • php_script.php
  • lt? php
  • firstname _GETfirstname //a variable is
    assigned the value
  • echo(Welcome to this website, firstname)
    //of firstname
  • ?gt //
    sent in the URL query string.

13
  • Passing multiple variable values with the GET
    method
  • lta hrefphp_script.php?fnamePaullnameSmithgtPa
    ul Smithlt/agt
  • Within php_script.php, these values would be
    accessed as _GET(fname) and _GET(lname),
    respectively.
  • Passing Values from Forms
  • GET Method
  • Similar to passing variables in URL query strings
    as demonstrated above.
  • PHP generates a _GET array for each named form
    element, and they can then be easily accessed by
    the PHP program.
  • Due to limitations in the number of characters
    that can be sent with the GET method, it is
    sometimes technically impossible to use this
    method to pass form data.

14
  • lthtmlgtltheadgtlttitlegtPassing form element values to
    PHPlt/titlegt
  • lt/headgt
  • ltbodygt
  • ltform methodGET actionphp_script.phpgt
  • ltinput typetext namefnamegtlt/inputgt
  • ltinput typetext namelnamegtlt/inputgt
  • ltinput typesubmitgt
  • lt/formgt
  • lt/bodygtlt/htmlgt
  • When the above form is submitted, it will
    generate an URL query string similar to href in
    the link, in the last example.
  • POST Method
  • The information is invisibly passed, such that it
    does not form part of the URL .

15
  • This method enables you to send data containing
    large values, e.g textarea, to the web server.
  • The variables passed are placed in another
    special array called _POST.
  • Example Here the GET method is replaced with
    POST
  • lthtmlgtltheadgtlttitlegtPassing form element values to
    PHPlt/titlegt
  • lt/headgt
  • ltbodygt
  • ltform methodPOST actionphp_script.phpgt
  • ltinput typetext namefnamegtlt/inputgt
  • ltinput typetext namelnamegtlt/inputgt
  • ltinput typesubmitgt
  • lt/formgt
  • lt/bodygtlt/htmlgt

16
  • The values in the form elements are accessed with
    the _POST array.
  • The php_script.php would have to be modified as
    shown below
  • php_script.php
  • lt? php
  • firstname _POSTfname
  • lastname _POSTlname
  • echo(Welcome to this website,ltbgt firstname
    lastname!lt/bgt)
  • ?gt
  • The _REQUEST array
  • Contains all variables that appear in both _GET
    and _POST
  • So youre no longer concerned about the method of
    the form.

17
  • Control Structures
  • PHP, like other programming languages provides
    facilities that allow us to control the sequence
    of actions in a script.
  • The if-else statement
  • Most common control structure.
  • Takes the general form
  • if (condition)
  • //statements to execute if
    condition is true
  • else // statement to execute if
    condition is false
  • The else statement is optional, i.e if there are
    no statements to execute if the condition is
    false, the else statement can be omitted.

18
  • E.g.
  • name _REQUESTname
  • if (name Paul)
  • echo (Welcome to this web site, master
    creator!)
  • else echo(Welcome to this website name)
  • Multiple conditions
  • In practice, you might want to combine more than
    one condition within the if construct.
  • This is achieved with the use of the and
    operator () and the or operator (). The word
    and is a legal operator in PHP.
  • E.g.
  • fname _REQUESTfname
  • lname _REQUESTlname
  • if (fname Paul lname Smith)
    // the condition evaluates to
  • echo (Welcome to this web site, master
    creator!) // true only if both
  • else echo(Welcome to this website fname
    lname) //statements are true.

19
  • The While Loop
  • Takes the form
  • while (condition)
  • / execute statements over and over as
    long as condition remains true /
  • Useful when youre processing a list of objects,
    e.g array elements.
  • E.g
  • count 1
  • while (count lt 10)
  • echo(This is element count)
  • count

20
  • The For Loop
  • Takes the form
  • for (initialize condition update)
  • / statements to execute in iterations, as
    long as condition
  • remains true after each update /
  • The counter variable is initialized, its
    condition set, and its value updated in the first
    line of the For Loop.
  • E.g.
  • for (count 1 count lt 10 count)
  • echo(This is element count)
  • References
  • Build Your Own Database Driven Website Using PHP
    MySQL. Kevin Yank. 2nd Ed. Chapter 3
  • Deitel, Deitel Nieto Chapter 29.
Write a Comment
User Comments (0)
About PowerShow.com