Developing Web Applications - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

Developing Web Applications

Description:

PHP is a server-side scripting language, which allows you to create dynamic Web pages ... Adrian: 95, Matty: 93, Lance: 56, Stephen: 70, Craig: 65, Andy: 98 ... – PowerPoint PPT presentation

Number of Views:94
Avg rating:3.0/5.0
Slides: 48
Provided by: ralphm5
Category:

less

Transcript and Presenter's Notes

Title: Developing Web Applications


1
Lecture 7 Introduction to PHP
2
Today
Introduction to PHP
  • PHP - Overview
  • Variables Scope
  • Output
  • Variable types Boolean Strings
  • String Functions Parsers
  • Screening User Input/Output
  • Maths functions
  • Control and flow

3
What is PHP?
  • PHP stands for PHP Hypertext Preprocessor
  • PHP is a server-side scripting language, which
    allows you to create dynamic Web pages
  • PHP scripts are executed on the server
  • PHP supports many databases (MySQL, Informix,
    Oracle, Sybase, Solid, PostgreSQL, Generic ODBC,
    etc.)
  • PHP is an open source software
  • PHP is free to download and use

4
Why PHP?
  • PHP runs on different platforms (Windows, Linux,
    Unix, etc.)
  • PHP is compatible with almost all servers used
    today (Apache, IIS, etc.)
  • PHP is FREE to download from the official PHP
    resource www.php.net
  • PHP is easy to learn and runs efficiently on the
    server side

5
What is a PHP File?
  • PHP files can contain text, HTML tags and scripts
  • PHP files are returned to the browser as plain
    HTML 
  • PHP files have a file extension of ".php",
    ".php3", or ".phtml

6
What do You Need?
  • If your server supports PHP you don't need to do
    anything. Just create some .php files in your web
    directory, and the server will parse them for
    you. Because it is free, most web hosts offer PHP
    support.
  • However, if your server does not support PHP, you
    must install PHP.
  • Download PHP http//www.php.net/downloads.php
  • Download MySQL http//www.mysql.com/downloads/ind
    ex.html
  • Download Apache Server http//httpd.apache.org/do
    wnload.cgi

7
Basic PHP Syntax
  • PHP code is executed on the server, and the plain
    HTML result is sent to the browser.
  • A PHP scripting block always starts with lt?php
    and ends with ?gt. A PHP scripting block can be
    placed anywhere in the document.
  • A PHP file normally contains HTML tags, just like
    an HTML file, and some PHP scripting code.
  • lthtmlgt ltbodygt lt?php echo "Hello World" ?gt
    lt/bodygt lt/htmlgt
  • Each code line in PHP must end with a semicolon.
    The semicolon is a separator and is used to
    distinguish one statement from another.

8
Basic PHP Syntax
  • There are two basic statements to output text
    with PHP echo and print.
  • Echo is not a function and does not return a
    value
  • echo "ltpgtThis is a paragraph.lt/pgt"
  • Print is a function and returns a value 1
    success, 0 failure
  • print("ltpgtThis is a paragraph too.lt/pgt")
  • Note The file must have the .php extension. If
    the file has a .html extension, the PHP code will
    not be executed.

9
Comments in PHP
  • In PHP, we use // to make a single-line comment
    or / and / to make a large comment block.
  • lthtmlgt
  • ltbodygt
  • lt?php //This is a comment
  • / This is a
  • comment block /
  • ?gt
  • lt/bodygt
  • lt/htmlgt

10
Variables in PHP
  • Variables are used for storing a values, like
    text strings, numbers or arrays.
  • When a variable is set it can be used over and
    over again in your script
  • All variables in PHP start with a sign symbol.
  • The correct way of setting a variable in PHP
  • var_name value
  • If you forget the sign at the beginning of the
    variable it will not work.
  • lt?php
  • txt "Hello World!"
  • number 16
  • ?gt

11
Variables in PHP
  • PHP is a Loosely Typed Language
  • In PHP a variable does not need to be declared
    before using it.
  • PHP automatically converts the variable to the
    correct data type, depending on how they are set.
  • In a strongly typed programming language, you
    have to declare (define) the type and name of the
    variable before using it.
  • In PHP the variable is declared automatically
    when you use it.

12
Variables in PHP
  • Variable Naming Rules
  • A variable name must start with a letter or an
    underscore "_"
  • A variable name can only contain alpha-numeric
    characters and underscores (a-z, A-Z, 0-9, and _
    )
  • A variable name should not contain spaces. If a
    variable name is more than one word, it should be
    separated with underscore (my_string), or with
    capitalization (myString)

13
Variables in PHP Scope
  • Scope refers to where within a script a variable
    has meaning or a value
  • Mostly variables are available to you anywhere
    within your script.
  • Note that variables inside a function are local
    to that function and cannot be accessed outside
    the function even if the function call and the
    called function are in the same script.
  • The modifier global allows function variables to
    be accessed outside the function

14
Variables in PHP types
  • String
  • Number
  • Integer
  • Real (float, decimal, double)
  • Boolean
  • TRUE / FALSE
  • Array
  • Object

15
Variables in PHP examples
  • Integer
  • a 1234 // decimal number
  • a -123 // a negative number
  • a 0123 // octal number (equivalent to 83
    decimal)
  • a 0x1A // hexadecimal number (equivalent to
    26 decimal)
  • Floating Point Numbers
  • a 1.234 a 1.2e3 a 7E-10
  • Boolean
  • foo True // assign the value TRUE to foo
  • // is an operator which returns a boolean
  • if (action
    "show_version")
  • echo "The
    version is 1.23"
  • // this is valid but
    not necessary
  • if (show_separators
    TRUE)
  • echo "lthrgt\n"
  • // because you can
    simply type this
  • if
    (show_separators)

16
Variables in PHP examples cont.
  • Strings (single or double quoted)
  • echo 'this is a simple string'
  • echo 'You can also have embedded newlines in
    strings, like this way.'
  • echo 'Arnold once said "I\'ll be back"'
  • // output ... "I'll
    be back"
  • echo 'Are you sure you want to delete C\.?'
  • // output ... delete
    C\.?
  • Arrays
  • error_descriptionsE_ERROR "A fatal error has
    occured"
  • error_descriptionsE_WARNING "PHP issued a
    warning"
  • error_descriptionsE_NOTICE "This is just an
    informal notice"
  • The above statements are in fact the same as the
    following, if E_ERROR, E_WARNING and E_NOTICE are
    defined as constants
  • error_descriptions1 "A fatal error has
    occured"
  • error_descriptions2 "PHP issued a warning"
  • error_descriptions8 "This is just an
    informal notice"

17
Constants and Global Variables
  • To define a constant
  • define(PI, 3.1416)
  • area PIradiusradius
  • Global variables defined outside any function
  • global var1, var2
  • function xyz()
  • localvarX var1

18
Arrays in PHP
  • An array can store one or more values in a single
    variable name.
  • When working with PHP, sooner or later, you might
    want to create many similar variables.
  • Instead of having many similar variables, you can
    store the data as elements in an array.
  • Each element in the array has its own ID so that
    it can be easily accessed.
  • There are three different kind of arrays
  • Numeric array - An array with a numeric ID key
  • Associative array - An array where each ID key is
    associated with a value
  • Multidimensional array - An array containing one
    or more arrays

19
Numeric Arrays
  • A numeric array stores each element with a
    numeric ID key.
  • There are different ways to create a numeric
    array.

20
Numeric arrays
  • Say that we have a list of marks out of 100 in a
    subject 95, 93, 56, 70, 65, 98
  • array value 1 - 95
  • array value 2 - 93
  • array value 3 - 56
  • array value 4 - 70
  • array value 5 - 65
  • array value 6 - 98
  • marks array (95, 93, 56, 70, 65, 98)
    generates a numerically-indexed array
  • marks0 95
  • marks1 93
  • marks2 56
  • marks3 70
  • marks4 65
  • marks5 98

21
Numeric arrays (cont)
  • The following code also generates a
    numerically-indexed array, allocating the next
    index after the highest current index to the
    element.
  • marks 95
  • marks 93
  • marks0 is 95 and marks1 is 93.
  • Note that array index starts at 0 by default.
  • You can skip indexes by allocating a specific
    index to a value -
  • marks5 56
  • marks 70
  • will be allocate 70 to marks6. Therefore
    marks5 is 56 and marks6 is 70.

22
Associative Arrays
  • In an associative array, each ID key is a string
    which is associated with a value.
  • When storing data about specific named values, a
    numerical array is not always the best way to do
    it.
  • With associative arrays we can use the names as
    keys and assign values to them.

23
Associative arrays
  • Suppose we have a list of marks in a subject and
    we want to represent who got what mark
  • Adrian 95, Matty 93, Lance 56, Stephen 70,
    Craig 65, Andy 98
  • marks array ("Adrian"gt93, "Lance"gt56,
    "Stephen"gt70, "Craig"gt65,
    "Andy"gt98)
  • Array marks maps a key to a value (or assign a
    value to a key)
  • name is the key
  • mark is the value

24
List an associative array
  • list() in conjunction with each() assigns the key
    and value of an array item into the variables
    key and value.
  • The following code prints each key / value pair
    into a table. Note that value might itself be an
    array.
  • reset(marks) //go to the beginning of the array
  • echo "lttable border\"1\"gt"
  • while (list(key, value) each(marks))
  • echo "lttrgtlttdgtkeylt/tdgtlttdgtvaluelt/tdgtlt/trgt\n"
  • echo "lt/tablegtlthrgt"

25
List an associative array (cont)
  • each() actually returns an array for each array
    item which includes the key and value with index
    0 mapped to the key and index 1 mapped to the
    value.
  • reset(marks)
  • while (row each(marks))
  • echo "Mark for row0 is row1ltbr /gt"

26
Multidimensional Arrays
  • In a multidimensional array, each element in the
    main array can also be an array. And each element
    in the sub-array can be an array, and so on.
  • families array
  • (
  • "Griffin"gtarray
  • ( "Peter", "Lois", "Megan" ),
  • "Quagmire"gtarray
  • ( "Glenn" ),
  • "Brown"gtarray
  • ( "Cleveland", "Loretta", "Junior" )
  • )

27
Strings
  • A string variable is used to store and manipulate
    a piece of text.
  • After we create a string we can manipulate it. A
    string can be used directly in a function or it
    can be stored in a variable.
  • We can manipulate strings by operators or
    functions.
  • There is only one string operator in PHP -- the
    concatenation operator (.), which is used to put
    two string values together.
  • lt?php
  • txt1"Hello World"
  • txt2"1234"
  • echo txt1 . " " . txt2
  • ?gt
  • There are many functions to manipulate strings

28
String functions
  • strlen() used to find the length of a string.
  • lt?php echo strlen("Hello world!") ?gt
  • The length of a string is often used in loops or
    other functions, when it is important to know
    when the string ends. (i.e. in a loop, we would
    want to stop the loop after the last character in
    the string)
  • strpos() used to search for a string or
    character within a string.
  • If a match is found in the string, this function
    will return the position of the first match. If
    no match is found, it will return FALSE.
  • lt?php
  • echo strpos("Hello world!","world")
  • ?gt

29
String functions others
  • boolean strcmp (str1, str2)
  • boolean strcasecmp (str1, str2)
  • boolean strstr (str1, str2)
  • boolean stristr (str1, str2)
  • int strlen(str)
  • string substr (str, start_pos, len)

30
String functions others (cont)
  • string chop (str)
  • string ltrim (str)
  • string trim (str)
  • string str_replace (old_txt, new_txt, text)
  • string substr_replace (old_txt, new_txt, text)
  • strtolower(str)
  • strtoupper(str)
  • ucfirst(str)
  • ucwords(str)

31
String functions formatting input/output
  • addslashes(str)
  • stripslashes(str)
  • escapeshellcmd(str)
  • strip_tags(str)
  • htmlspecialchars(str)
  • htmlentities(str)
  • nl2br(str)

32
PHP Operators
  • Operators are used to operate on values.
  • Arithmetic Operators
  • - /
  • --

33
PHP Operators
  • Assignment Operators
  • -
  • /
  • .

34
PHP Operators
  • Comparison Operators
  • !
  • gt
  • lt
  • gt
  • lt

35
PHP Operators
  • Logical Operators
  • !

36
Maths functions
  • low_int floor (double)
  • high_int ceil (double)
  • nearest_int round (double)
  • (nearest even number if exactly .5)
  • positive abs (number)
  • min min (n1, n2 , nn)
  • max max (n1, n2 , nn)

37
Control and flow
  • if (expr1)
  • elseif (expr2)
  • else
  • while (cond)
  • do while (cond)
  • switch (var)
  • case a
  • case b
  • for (i 0 i lt expr i )
  • foreach (array_expr as value)
  • foreach (array_expr as keygtvalue)
  • break
  • continue

38
If.. Then.. else
  • if (a gt b) print "a is bigger than b"
  • if (a gt b)
  • print "a is
    bigger than b"
  • b a
  • if (a gt b)
  • print "a is
    bigger than b"
  • elseif (a b)
  • print "a is
    equal to b"
  • else
  • print "a is
    smaller than b"

39
While
  • / example 1 /
  • i 1
  • while (i lt 10)
  • print i /
    the printed value would be

  • i before the increment

  • (post-increment) /
  • / example 2 - alternative notation to using
    the braces - and endwhile/
  • i 1
  • while (i lt 10)
  • print i
  • i
  • endwhile

40
For loops
  • / example 1 similar to C syntax / for
    (i 1 i lt 10 i)
  • print i
  • / example 2 / for
    (i 1i)
  • if (i gt 10)
  • break
  • print i
  • / example 3 / i 1
  • for ()
  • if (i gt 10)
  • break
  • print i
  • i
  • / example 4 / for
    (i 1 i lt 10 print i, i)

41
Foreach
  • An easy way to iterate over arrays. There are two
    syntaxes -- the second is a minor but useful
    extension of the first
  • foreach(array_expression as value)
  • foreach(array_expression as key gt value)
  • The following are functionally identical
  • //example 1 //
  • reset (arr)
  • while (list(, value) each
    (arr))
  • echo "Value valueltbrgt\n"
  • //example 2 //
  • foreach (arr as value)
  • echo "Value valueltbrgt\n"

42
Foreach cont.
  • / foreach example 1 value only /
  • a array (1, 2, 3, 17)
  • foreach (a as v)
  • print "Current value of \a v.\n"
  • / foreach example 2 value (with key printed
    for illustration) /
  • a array (1, 2, 3, 17)
  • i 0 / for illustrative purposes only /
  • foreach(a as v)
  • print "\ai gt v.\n"
  • i
  • / foreach example 3 key and value /
  • a array ( "one" gt 1, "two" gt 2, "three" gt
    3, "seventeen" gt 17 )
  • foreach(a as k gt v)
  • print "\ak gt v.\n"

43
Break Continue
  • break ends execution of the current for, foreach,
    while, do..while or switch structure.
  • arr array ('one', 'two', 'three', 'four',
    'stop', 'five')
  • while (list (key, val) each (arr))
  • if (val 'stop')
  • break / You could also write 'break
    1' here. /
  • echo "valltbrgt\n"
  • / note list() is a multiple assignment function
    the key and value returned by each() are assigned
    to key and value. key is not used in this
    example./
  • continue is used within looping structures to
    skip the rest of the current loop iteration and
    continue execution at the beginning of the next
    iteration.

44
Switch
  • if (i 0) print "i equals 0"
  • if (i 1) print "i equals 1"
  • if (i 2) print "i equals 2"
  • / this is equivalent /
  • switch (i)
  • case 0
  • print "i equals 0"
  • break
  • case 1
  • print "i equals 1"
  • break
  • case 2
  • print "i equals 2"
  • break

45
require() and include()
  • require() includes and evaluates a specific file.
  • require() and include() are identical in every
    way except how they handle failure. include()
    produces a warning while require() results in a
    fatal error.
  • lt?php
  • require 'prepend.php'
  • require somefile
  • require ('somefile.txt')
  • ?gt
  • require_once() or include_once() should be used
    in cases where the same file might be included
    and evaluated more than once during a particular
    execution of a script, and you want to be sure
    that it is included exactly once to avoid
    problems with function redefinitions, variable
    value reassignments, etc.

46
What we covered today
  • Introduction to PHP
  • Variables, constants, arrays and strings
  • Control structures sequence, repetition and
    selection

47
Next More advanced PHP
Write a Comment
User Comments (0)
About PowerShow.com