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 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:
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.
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.
Script 6-2: Using if conditionals and the empty() function, this PHP script begins checking whether the form was properly completed.
To create an if conditional:
- Create a new PHP script (Script 6-2) in Brackets with the title Registration:
- Begin the PHP section, and address error handling, if necessary:
- Assign all the form values to local variables, in case register_globals is turned off:
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!) - Print an introductory message:
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). - Validate the first name value:
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. - Repeat the validation for the last name, email address, and password:
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. - Complete the PHP section and the HTML page:
- Save the file as handle_reg.php to your XAMPP folder (c:\XAMPP\htdocs\202\chapter6).
- 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...
Figure 6-5: ...results in just this.
Note: You will have to use register.html to input (or omit) values. - 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.
- 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:
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: