Chapter 6 Exercises

Exercise 2: The if Conditional

The basic programming conditional is the standard if (what used to be called an if-then conditional—with the then now implied). The syntax for this kind of conditional is very simple:

The syntax for "if" conditional in PHP.

The condition must go within parentheses, and then you begin the statement(s) area after a curly brace. Executable commands must go in the statement(s) section of the conditional (for example, printing a string or adding two numbers together). Each separate statement (or command) must have its own semicolon indicating the end of the command line, but there is no limit on the number of statements that can result from a conditional. The statement(s) section is closed with a second curly brace.

Programmers commonly indent these statements from the initial if line to indicate that they are the result of a conditional, but that format is not syntactically required. Some people also use this syntax:

Another syntax for "if" conditional in PHP.

Failure to use a semicolon after each statement, forgetting an opening or closing parenthesis or curly brace, or using a semicolon after either of the braces will cause errors to occur. So, be mindful of your syntax as you code with conditionals.

PHP uses the concepts of TRUE and FALSE when determining whether to execute the statements. If the condition is TRUE, the statements are executed; if it is FALSE, they are not executed.

The example for validation functions in PHP.


To begin working with the if conditional, you will create the handle_reg.php script, which will receive data from register.html. In this exercise you will use one of the functions explained in the Validation Functions write-up (above), to ensure end users will fill out the registration correctly.

Note: A number of Helpful Tips are located at the end of this web page for reference as you complete this exercise.

Script 6-2: Using if conditionals and the empty() function, this PHP script begins checking whether the form was properly completed.

The PHP script that checks whether the form was properly completed using if conditionals and the empty() function as displayed in the editor view.

To create an if conditional:

  1. Create a new PHP script (Script 6-2) in Brackets with the title Registration:
    A new HTML document with the title Registration as displayed in the editor view.
  2. Begin the PHP section, and address error handling, if necessary:
    The PHP code snippet that begins the PHP section and addresses error handling as displayed in the editor view.
  3. Assign all the form values to local variables, in case register_globals is turned off:
    The PHP code that assigns all the form values to local variables as displayed in the editor view.
    To avoid complications resulting from register_globals being turned off, you need to assign each $_POST variable to a new variable within the script. You use $_POST here because you used the POST method in the form (and remember that the capitalization of variables counts, so you cannot use $_post or First_Name). (This will make more sense after you read Chapter 7, Using Arrays!)
  4. Print an introductory message:
    The PHP print function that prints an introductory message as displayed in the editor view.
    In truth, the only purpose of this message is to prevent a blank page from displaying after the form is filled out completely (as, in that case, no error messages will be printed).
  5. Validate the first name value:
    The PHP code snippet that validates the first name values as displayed in the editor view.
    This if conditional uses the code empty ($first_name) as its condition. If the $first_name variable is empty, meaning it has no value, a value of 0, or a value of an empty string, the conditional is TRUE, and the print() statement is executed. If the $first_name variable is not empty, then the conditional is FALSE, and the print() function is never called.
  6. Repeat the validation for the last name, email address, and password:
    The PHP code snippet that validates the last name email address, and password as displayed in the editor view.
    Each of these is a repeat of the first name validation, but here you specify the different variable names and change the print() statements accordingly. You do not need to validate the other form inputs using this syntax for reasons you will see as the chapter progresses.
  7. Complete the PHP section and the HTML page:
    The closing tags that completes the PHP section and the HTML page as displayed in the editor view.
  8. Save the file as handle_reg.php to your XAMPP folder (c:\XAMPP\htdocs\202\chapter6).
  9. Using Cyberduck, upload the file to the production server. Make sure it is in the same place as register.html, and ensure you are looking at the production server version of the file, then test it in your browser (Figure 6-4 and 6-5).
    Figure 6-4: Filling out the HTML form completely...
    The HTML form as displayed in the browser view.
    Figure 6-5: ...results in just this.
    The empty result page as displayed in the browser view.

    Note: You will have to use register.html to input (or omit) values.
  10. Resubmit the form in different states of completeness to test the results some more (Figure 6-6).
    Figure 6-6: If you omit the first name, last name, email address, or password during input, you will see messages like these.
    The validation messages that show the omitted the first name, last name, email address, password in the browser view.
Important: If something doesn’t look right in your document, and you need to make changes to the script, follow the editing procedure using Synapse outlined in Chapter 1.

 

Quick tips icon

  • If the statement area of your conditional is only one line long, you technically do not need the curly braces. In that case, you can place the whole conditional on one line, like so:
    The one-line "if" conditional without braces.
    You may run across code in this format. However, it is best to always use the multiline format (as demonstrated in the syntax introduction) to improve consistency and minimize errors.
  • When you use functions within your conditionals, as you use empty() here, it can be easy to forget using a closing parenthesis and you may see a parse error. Be extra careful with your syntax when you are coding any control structure.
  • One use of the isset() function is to avoid referring to a variable unless it exists. If PHP is set to report notices (see Chapter 3, Exercise 5: Error Reporting), then, for example, using $var if it has not been defined will cause an error. You can avoid this by coding:
    The "if" conditional using the isset() function.

Return to Chapter 6, Assignments Page