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 PHP code snippet that links the item using the period(.).

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:

The PHP code snippet using variables with the double quotes.

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.

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

To use concatenation:

  1. 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.
    The PHP code using one of the most common manipulations of a string variable as displayed in the editor view.
  2. To begin, create a new HTML document (Script 5-2) with the title Your Forum Posting:
    The HTML tags with title Your Forum Posting as displayed in the code editor view.
  3. Create the initial PHP tag, and address PHP’s error handling, if necessary:
    The PHP code snippets that create the initial PHP tag, and addresses PHP's error handling as displayed in the editor view.
    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.
  4. Address the register_globals issue, if necessary:
    The PHP code snippets that addresses the register_globals issue as displayed in the editor view.
    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.
  5. Create a new $name variable using concatenation:
    The PHP code snippet that creates a new $name variable using concatenation in the editor view.
    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.
  6. Print out the message to the user:
    The PHP code that message reports back to the user what they entered in the form as displayed in the editor view.
    This message reports back to the user what they entered in the form.
  7. Close the PHP section and complete the HTML code:
    The closing tags of HTML and PHP as displayed in the editor view.
  8. Save the script as handle_post.php to your XAMPP folder (c:\XAMPP\htdocs\202\chapter5).
  9. 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.
    The testing HTML form to input data as displayed in the browser view.
    Figure 5-3: Testing the form one more time.
    The result after submitting the HTML form as displayed 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 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.

    The PHP code depicting how a number variable can be turned into a string.

  • In this example you could also have written:
    The PHP code that is recommended for concatenation in PHP.
    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:
    The PHP code that is recommended for concatenation in PHP.

  • Concatenation can be used in many ways, even when you are feeding arguments to a function. An uncommon but functional example would be:
    The functional way of concatenation in PHP.

Return to Chapter 5, Assignments Page