Control Structures in PHP - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Control Structures in PHP

Description:

foreach(array_expression as $key = $value) statement. Jumping in and ... form method='POST' action='http://student.cs.ucc.ie/cs4400/jabowen/php/file012.php' ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 13
Provided by: BryanD81
Category:

less

Transcript and Presenter's Notes

Title: Control Structures in PHP


1
Control Structures in PHP
  • Bryan Duggan

2
Control Structures if statements
  • if (a gt b)
  • echo "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"
  • else print "a is NOT bigger than b"
  • 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

3
Example usage
  • Example
  • lthtmlgt
  • ltheadgtlttitlegtYour browserlt/titlegtlt/headgt
  • ltbodygt
  • lth1gtYour Browserlt/h1gt
  • ltpgt
  • lt?php
  • if( strstr(HTTP_USER_AGENT,"MSIE") )
  • echo "You are using Internet
    Explorer"
  • ?gt
  • to view this page.
  • lt/pgt
  • lt/bodygt
  • lt/htmlgt
  • strstr is a function which checks if its 2nd
    argument is a substring of its 1st

4
Control constructs -- while
  • These are just like their counterparts in C
  • i 1
  • while ( i lt 10 )
  • echo i
  • i 0
  • do print i while (igt0)

5
Control constructs -- for
  • These are just like their counterparts in C
  • for (i 1 i lt 10 i)
  • print i

6
Control constructs -- foreach
  • These are similar their counterparts in Perl
  • foreach(array_expression as value)
  • statement
  • foreach(array_expression as key gt value)
  • statement

7
Jumping in and out of PHP mode
  • We can can jump in and out of PHP mode even in
    the middle of a PHP block
  • lt?php
  • if(strstr(HTTP_USER_AGENT,"MSIE"))
  • ?gt ltpgtYou are using Internet
    Explorerlt/pgt lt?php
  • else ?gt ltpgtYou are not using Internet
    Explorerlt/pgt lt?php
  • ?gt
  • Instead of using an echo statement to print
    something, we jumped out of PHP mode.
  • Note that the logical flow of the PHP remains
    intact
  • Only one of the HTML blocks will be sent to the
    user.

8
A FORM and its handler in one file
  • lthtmlgt
  • ltheadgt
  • lttitlegtApplication Handlerlt/titlegt
  • lt/headgt
  • ltbodygt
  • lt?php
  • if (!_POSTsurname)
  • ?gt ltform method"POST"
  • action"http//student.cs.ucc.ie/cs4400/jabo
    wen/php/file012.php"gt
  • ltpgtYour surname ltinput type"text"
    name"surname"gtlt/pgt
  • ltpgtYour address ltinput type"text"
    name"address"gtlt/pgt
  • ltbutton type"submit"gtPlease send me the
    brochure.lt/buttongt
  • lt/formgt
  • lt?php
  • else echo "ltpgtThank you, surname.lt/pgt"
  • echo "ltpgt We will write to you at
    address.lt/pgt" ?gt
  • lt/bodygt
  • lt/htmlgt

9
Finding out about your PHP environment
  • One of the many pre-defined PHP functions is
    phpinfo()
  • lthtmlgt
  • ltbodygt
  • lth1gtYour PHP Environmentlt/h1gt
  • lt?php phpinfo() ?gt
  • lt/bodygt
  • lt/htmlgt
  • In what follows, notice that mySQL support is
    enabled

10
Adding Comments to a PHP Script
  • Comments are nonprinting lines placed in code
    such as
  • The name of the script
  • Your name and the date you created the program
  • Notes to yourself
  • Instructions to future programmers who might need
    to modify your work

11
Adding Comments to a PHP Script (continued)
  • Line comments hide a single line of code
  • Add // or before the text
  • Choose and stick with version
  • Block comments hide multiple lines of code
  • Add / to the first line of code
  • And / after the last character in the code

12
Example Comments
  • lt?php
  • /
  • This line is part of the block comment.
  • This line is also part of the block comment.
  • /
  • echo (lth1gtComments Examplelt/h1gt) // Line
    comments can follow
  • code statements
  • // This line comment takes up an entire line.
  • This is another way of creating a line comment.
  • / This is another way of creating
  • a block comment. /
  • ?gt
Write a Comment
User Comments (0)
About PowerShow.com