Working with Conditions

Duration: 20 to 30 minutes.

In this exercise, you will create a page for handling a simple form submission.

Open FlowControl/Exercises/Greeting.html in your editor and review the code. This is the form that will be submitted. Filled out, it looks like this: This form is submitted using the get method, which means the form entries will be appended to the query string and will be accessible in the $_GET array.

HTML forms will be discussed more later in the course.

Open FlowControl/Exercises/Greeting.php in your editor.

Insert a PHP block that checks to see if the user filled
out both the LastName and the Gender fields in the form.

If the user failed to fill out either one of the fields,
write out an error message to the screen.

If the user filled out both fields, return a gender-appropriate
greeting such as "Hello Mr. Dunn!" or "Hello Ms. Dunn!" If the gender is not recognizable (e.g, not male or female),
return an error message.

Try to use both an if condition and a switch statement in this exercise.

When you are done, test your solution in your browser.

Challenge

It is likely that your solution does not take into account different cases for the gender. For example, "MALE" and "FEMALE" may not be recognized as valid genders. Try to find the function at http://www.php.net that can be used for converting the user's entry to all lowercase letters and use that function to make the gender check case insensitive.