COMP6015 - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

COMP6015

Description:

COMP6015 An Introduction to Computer Programming Lecture 04 – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 14
Provided by: TaZ53
Category:

less

Transcript and Presenter's Notes

Title: COMP6015


1
  • COMP6015
  • An
  • Introduction
  • to
  • Computer Programming
  • Lecture 04

2
Introduction to PHP
  • Arrays
  • The built-in function count() tells you how many
    elements are in an array
  • fruit array('banana','papaya')
  • print count(fruit)
  • Prints 2

3
Introduction to PHP
  • Control Structures
  • You can use looping structures such as for and
    while
  • for (i 4 i lt 8 i)
  • print "I have eaten i bagels today.\n"
  • prints
  • I have eaten 4 bagels today.
  • I have eaten 5 bagels today.
  • I have eaten 6 bagels today.
  • I have eaten 7 bagels today.

4
Introduction to PHP
  • Control Structures
  • So does
  • i 4
  • while (i lt 8)
  • print "I have eaten i bagels today.\n"
  • i

5
Introduction to PHP
  • Control Structures - if and elseif
  • if (user_count gt 200)
  • print "The site is busy right
    now!"
  • elseif (user_count gt 100)
  • print "The site is sort of active right
    now!"
  • else
  • print Only user_count user logged on."

6
Introduction to PHP
  • Control Structures - if and elseif
  • (Code is also written this way)
  • if (user_count gt 200)
  • print "The site is busy right
    now!"
  • elseif (user_count gt 100)
  • print "The site is sort of active right
    now!"
  • else
  • print Only user_count user
    logged on."

7
Introduction to PHP
  • Control Structures
  • The rule of thumb about operators also applies to
    control structures.
  • You can use
  • switch
  • do...while
  • even the ?
  • construct.

8
Introduction to PHP
  • Control Structures
  • Switch
  • switch(var1)
  • case 1 print(We found one\n)
  • case 2 print(We found two\n)
  • case 3 print(We found three\n)
  • default print(We did not find any!\n)

9
Introduction to PHP
  • Control Structures
  • dowhile
  • do
  • print(var times\n)
  • var
  • while (var lt var2)

10
Introduction to PHP
  • Control Structures
  • Ternary operator (? )
  • print ( (var gt var2) ? var var2)
  • OR
  • var (test_var "December") ?
  • "Merry Christmas" "It is not Christmas
    yet"

11
Introduction to PHP
  • One of PHP's most handy features is its ability
    to automatically load the values of variables
    from forms into variables in PHP. This makes form
    processing fairly easy.
  • So, a submitted form with input field such as
    this
  • ltINPUT TYPETEXT NAME"name" VALUE"Glen
    Morris"gt
  • when processing the page with PHP, the variable
    name has a value of Glen Morris.
  • So, we can do things such as
  • echo "Hi name!"
  • or test its value
  • if (name "Glen Morris")
  • echo "Please check your email."

12
Introduction to PHP
  • NOTE
  • Forms manipulation
  • will be covered in
  • much more detail in
  • a subsequent course.

13
Introduction to PHP
  • EXERCISE
  • Develop PHP code to address the following
  • Insert the numbers 1 20 into an array
  • Print out the even numbers from the array - 5
    values to a line
  • Print out the odd numbers from the array 4 to a
    row, starting with the largest.
  • Output the numbers from the array 1 to a row,
    but if the value is a multiple of 4, output the
    word BINGO instead.
Write a Comment
User Comments (0)
About PowerShow.com