Conditionals Statements - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Conditionals Statements

Description:

The if statement is the most basic conditional statement in PHP ... echo 'Spend some time in the hammock'; break; default: echo 'Better get to work!'; 9/6/09 ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 7
Provided by: davema77
Category:

less

Transcript and Presenter's Notes

Title: Conditionals Statements


1
Conditionals Statements
  • if, else, elseif, and switch

2
if
lt? x 12 y 10 if(x gt y)
echo x is bigger than y ?gt
  • The if statement is the most basic conditional
    statement in PHP and other programming languages.
  • if(condition)
  • statement

3
else
lt? if(x gt y) echo x is bigger than
y else echo x is NOT bigger than y
y x ?gt
  • else allows you to specify what happens if the
    first condition is not met.
  • if(condition)
  • statement
  • else
  • alternate statement

4
elseif
lt? if(x gt y) echo x is bigger than
y elseif(x lt y) echo x is smaller
than y else echo x is equal to y
?gt
  • Use elseif to put multiple conditional
    statements into the same block of code.
  • if(condition)
  • statement
  • elseif(alternate condition)
  • alternate statement
  • else
  • alternate statement

5
switch
lt? switch(dayofweek) case "Saturday"
echo "Time for a BBQ" break case "Sunday"
echo "Spend some time in the hammock"
break default echo "Better get to
work!" ?gt
  • If you have many elseif statements in a code
    block, it is probably better to use a switch
    statement.
  • switch(expression)
  • case value
  • statement
  • break
  • case value
  • statement
  • break
  • default
  • statement

6
Exercise
  • Create an HTML form with one text box. Instruct
    the user to type a number between 10 and 20
    (inclusive) in the text box.
  • Create a PHP script that catches the users entry
    and displays a message explaining whether the
    answer is correct or incorrect.
  • Add one more feature you think this script needs
    (for example, check to see if a non-numeric value
    was entered).
  • Extra credit Create a second version of your
    script that uses a switch statement.
Write a Comment
User Comments (0)
About PowerShow.com