Previous --> Page 3 |This is Page 4 | Next --> Page 5

Making Decisions

We can use IF STATEMENTS to make decisions. These decisions include how the page should appear as well as what content to display. Note that this is the very basis of a dynamic page that is tailored to the user. The true power of PHP pages of this form is that it enables the programmer to only make ONE page that can be different depending upon the data it is sent.

IF STATEMENT SYNTAX

if(<<condition>>)
{
  // a line of code
  // another line of code
}

The condition must evaluate to either true or false. Note that if the condition is false, execution bypasses the code between the braces and continues below them.

IF/ELSE SYNTAX
Sometimes you want something to happen one way or another, depending upon the evaluation of a condition. In this setting, the syntax is as follows:

if(<<condition>>)
{
  // a line of code
  // another line of code
}
else // condition was false, do below
{
  // code
}

PUTTING IT INTO PRACTICE - Two sample blocks of code

if($_POST['Age'] > 25)
{
  echo "You are ancient.";
}


if($_POST['DogsOrCats'] == "Dogs")
{
  echo "You like dogs.";
}
else
{
  echo "Cats are your thing!";
}

 

ASSIGNMENT 4 - Name your file php4.php and php4process.php

This assignment will be a continuation of the third assignment. To begin, re-save phpDynamic.php (from page 3) as php4.php. Change the action in the form on php4.php to be php4process.php. (php4.php should ask for their name/gender/fav color)

Your task is to create a webpage that displays the users information, but with a slight twist. It will require the use of IF statements.

Your page should do the following:

- Display all information as typed on the form
- If the user is female, your page should display the text, "Girls are cooler than guys!"
- If the user is male, your page should display instead, "Guys are cooler than girls!"
- Lastly, the background color of your webpage should be the user's favorite color. You may need to incorporate Javascript and PHP here.