PHP Tutorials

The String Concatenation Operator

Like the numeric variable types, strings can be used with operators. However, operators don’t treat strings the same way they treat numeric variables. For example, multiplying two strings together wouldn’t make much sense. Neither would dividing, adding, or subtracting them.

TIP

You may be thinking, “Hey, wait. Wouldn’t adding strings ‘glue’ them together to form a composite?” This operation of “adding” strings, though, is known among programmers as concatenation, which has a separate operator. We’ll discuss that a little later in this section.

In fact, if you perform a mathematical operation on strings, the strings are typecast to the most appropriate numeric type before the expression evaluates them.

To refresh your memory on string-to-numeric typecasting, see “Type Casting,”.

The string concatenation operator, however, is made to work with strings. Concatenation is the joining together of two or more strings to form a single string. The string concatenation operator is the period; so, to join two strings together, we place a string on either side of a period. The resulting string can either be stored in a third string by assignment, or echoed directly to output.

NOTE

This works the same way as adding 1 + 2 + 3. You can have any number of concatenation operators in an expression, as long as each has a string on either side.

For example, if you ask a user for his first and last names as two separate entries, then you can use concatenation to display his full name in whatever format you prefer, or even switch back and forth within the same script as necessary.

The following program asks the user for his first and last names, then prints his full name in two different formats:



<?php
/* File: ch05ex01.php – demonstrates string concatenation */

?>
<html>
<head><title>PHP By Example :: Chapter 5 :: Example 1</title></head>

<body>

<?php

if ($HTTP_GET_VARS['fname'] && $HTTP_GET_VARS['lname'])
{
echo $HTTP_GET_VARS['fname'] . ' ' . $HTTP_GET_VARS['lname'] . '
';
echo $HTTP_GET_VARS['lname'] . ', ' . $HTTP_GET_VARS['fname'] . '
';
}

?>

<form action="ch05ex01.php" method="GET">
First Name: <input type="text" name="fname">

Last Name: <input type="text" name="lname">

<input type="submit">
</form>

</body>

</html>

Being able to join two strings in this manner expands the flexibility of your program. Because you know that this is possible, you can ask for first and last names separately, and, in turn, have the ability to manipulate that name however you want.

For example, let’s say you’re organizing a convention (such as the annual ApacheCon) for which participants will sign up through your Web site. By gathering people’s first and last names independently of each other (instead of gathering one, inflexible fullname), you have the ability to format and sort the names however you want.

In some instances—creating a roster of attendees, for example—you might want to list the names alphabetically by last name. To do so, you simply concatenate the last name, a string containing a comma and a space, and the first name, like so:

$name_full = $name_last . ', ' . $name_first;

The resulting string might look like this:

Smith, John

Later in this chapter, you will learn the skills needed to create a string containing a person’s initials using the strings containing his first and last names. (For example, given that a person’s first name is Joe and his last name is Smith, you could find that his initials are J.S.) For now, you should have a clear idea of how string concatenation works and how to do it.

Filed under: Chapter 5 @ 6:58 pm

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress