Connecting Strings (Concatenation)

I l @ ve RuBoard

It's an unwieldy term , but a useful concept concatenation. It refers to the process of linking items together. Specifically in programming, you concatenate strings. The period (.) is the operator for performing this action, and it's used like so:

 $NewString = $aString . $bString. 

You can link as many strings as you want in this way. You can even join numbers to strings:

 $NewString = $aString . $bString   $cNumber; 

This works because PHP is weakly typed, meaning that its variables are not locked in to one particular format. Here, the $cNumber variable will be turned into a string and appended to the value of the $NewString variable.

Your HandleForm.php script contains strings that could logically be concatenated . It's quite common and even recommended to take a user 's first and last name as separate inputs, as you did with your form. On the other hand, it would be advantageous to be able to refer to the two together as one name . You'll modify your script with this in mind.

To use concatenation in your script:

  1. Open HandleForm.php in your text editor (Script 5.2).

  2. Change line 11 to read:

     $Name = $FirstName . " "   $LastName; (Script 5.3). 
    Script 5.3. Concatenation is one of the most common manipulations of a string variable. Think of it as addition for strings.

    graphics/05sc03.jpg

  3. Since you'll be joining the two names into one, you'll no longer need separate print statements (lines 11 and 12 of Script 5.2), so you'll replace this line and then modify the next . You have also made sure that the concatenation takes place after the trim() statements, because you cannot delete extra spaces from within a string using trim() once you've concatenated the two names together. Last, you have inserted a space between the two names so that they do not run together.

  4. Change line 12 to:

     print ("Your name is $Name.<BR>\n"); 
  5. Save your script, upload it to your server, and test in your Web browser (Figures 5.6 and 5.7).

    Figure 5.6. Although the HTML form itself won't change, the HandleForm.php script will be able to create the user's full name out of the separate values entered here (see Figure 5.7).

    graphics/05fig06.gif

    Figure 5.7. Via a simple concatenation of the two name variables, you are able to derive a more useful variablethe user's full name.

    graphics/05fig07.jpg

Tip

Due to the nature of how PHP deals with variables, the same effect could be accomplished using $Name = "$FirstName $LastName";. This is because 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 stringsas you did hereis more commonly used and I therefore recommend doing it that way (it will be more obvious what is occurring in your code).


Tip

You could also have written $FirstName = $FirstName . " " . $LastName; but there are two reasons why you shouldn't. First, doing so would have overwritten the original value of the $FirstName variable. Second, "FirstName" would no longer be an appropriate description of the variable's value. You should always try to maintain valid variable names as you program.


I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net