Chapter 5 Exercises
Exercise 2: Connecting Strings (Concatenation)
Concatenation is an unwieldy term but a useful concept. It refers to the process of linking items together. More specifically, in programming, you concatenate strings. The period (.) is the operator for performing this action, and it is used as follows:
The end result of this concatenation is that the $greeting variable has a value of Hello, world!
Due to the way PHP deals with variables, the same effect could be accomplished using:
This approach, however, would add an extra space to the phrase. Variables used within double quotation marks are replaced with their value when handled by PHP. However, the formal method of using the period to concatenate strings is more commonly used and is recommended. (It will be more obvious by using the period that is occurring in your code.)
The posting.html script sends several string variables to the handle_post.php page. Of those variables, the first and last names could logically be concatenated. It is quite common and even recommended to take a user’s first and last name as separate inputs, as you did with this form. On the other hand, it would be advantageous to be able to refer to the two together as one name.
In this exercise you will write the PHP script with this in mind.
To use concatenation:
- You will create a PHP script (Script 5-2).
Script 5-2: This PHP script demonstrates concatenation, one of the most common manipulations of a string variable. Think of it as addition for strings.
- To begin, create a new HTML document (Script 5-2) with the title Your Forum Posting:
- Create the initial PHP tag, and address PHP’s error handling, if necessary:
These lines will not be necessary for all users, but they are included here just in case. You can always refer back to Chapter 3, HTML Forms and PHP, to review the information explaining when, why, and how to use these lines. - Address the register_globals issue, if necessary:
As introduced in Chapter 3: HTML Forms and PHP, if register_ globals is disabled in your PHP configuration, you will need to include these lines in order to access the $first_name, $last_name, and $posting variables. All of this will be further explained in Chapter 7, Using Arrays. This example does not have a line for the email address because you will not be using it yet, but you can duplicate this code to include the email address as well. - Create a new $name variable using concatenation:
This act of concatenation takes two variables plus a space and joins them all together to create a new variable, called $name. For example, assume you entered Elliott and Smith as the names, then $name would be equal to Elliott Smith. - Print out the message to the user:
This message reports back to the user what they entered in the form. - Close the PHP section and complete the HTML code:
- Save the script as handle_post.php to your XAMPP folder (c:\XAMPP\htdocs\202\chapter5).
- Using Cyberduck, upload the file to the production server, and test in your browser (Figures 5-2 and 5-3), ensuring you are looking at the production server version of the file, and ensuring it is saved in the same location as posting.html.
Figure 5-2: Testing the form one more time.
Figure 5-3: Testing the form one more time.
- If you used quotation marks of any kind in your form and saw extraneous slashes in the printed result, read the next section of this chapter to see why.
- As a reminder, it is very important to know the difference between single and double quotation marks in PHP. Characters within single quotation marks are treated literally; characters within double quotation marks are interpreted (for example, a variable’s name will be replaced by its value). See Chapter 3, HTML Forms and PHP for a refresher.
- You can link as many strings as you wish using concatenation. You can even join numbers to strings:
This works because PHP is weakly typed, meaning that its variables are not locked in to one particular format. Here, the $number variable will be turned into a string and appended to the value of the $new_string variable.Note: Using single or double quotes in the below example will produce the same result. - In this example you could also have written:
However, this is not recommended for two reasons. First, doing so would overwrite the original value of the $first_name variable. Second, $first_name would no longer be an appropriate description of the variable’s value. You should always try to maintain valid variable names as you program.In this example, if you wanted to concatenate the $first_name and $last_name fields, a more appropriate variable name would be $full_name:
- Concatenation can be used in many ways, even when you are feeding arguments to a function. An uncommon but functional example would be: