Combining Strings5:34 with Alena Holligan
Combining data in a string is called concatenation. Besides using double quotes to add another variable into a string, PHP string concatenation also provides two string operators.
There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information.
To summarize concatenation. If you want to add a single variable to a string you can include that variable within your double quoted assignment. If you are concatenating a single line you would use a single dot between each string. If you are concatenating multiple lines, you should use the .= assignment operator.
Combining data in a string is called concatenation. 0:00 We perform concatenation in the last video 0:04 by adding one string variable to another string value. 0:07 By using double quotes, we included the first variable 0:10 into the string being assigned to the second variable. 0:14 PHP also provides two string operators for concatenation. 0:16 The dot and the dot equals. 0:22 Let's take a look at how to use these operators for more concatenation options. 0:25 The first string operator for concatenation is a single dot, or period. 0:30 The dot allows us to add as many strings together as we want in one statement. 0:35 For example, let me duplicate this line, then I can show you how to do 0:40 this same thing using single quotes, but still show the value of the variable. 0:44 We end this string here and concatenate with a single dot, 0:49 concatenate again, and start our final string. 0:54 Our string works, and our variable value is displayed. 1:03 But our escape sequences aren't working properly. 1:06 We can remove the escape character from before the double quotes, and 1:10 then we can concatenate this escape sequence, the /n, to the end. 1:15 Now when we run our script, just like we want. 1:28 Remember, PHP doesn't care about extra white spaces. 1:32 So as long as you haven't finished your statement with a semicolon, 1:35 you can split this line up however you want. 1:38 Be aware that if you add a line break within the quotes, 1:50 that line break will be displayed. 1:53 This could be a good or bad thing, depending on the desired results. 1:56 Early on I said, with great power comes great responsibility. 2:01 So just because you can concatenate your string over multiple lines 2:05 doesn't mean that you should, splitting up a statement 2:09 into multiple lines can make it extremely hard to follow. 2:12 If you are going to split the line, try to make it as easy to read as possible. 2:15 Indent the next lines and start each line with a concatenation character. 2:20 Notice that extra space right before Hello Alena? 2:39 That's because our string is split between two lines, and it's within single quotes. 2:43 So it's trying to display the characters exactly as they're shown. 2:49 I could end my string here, and then start my string again down here. 2:53 Now we have a single line. 3:03 Now this is easier to read, but 3:05 it's probably not the best choice if you're going to use multiple lines. 3:07 You're probably better off using the second operator, the dot equals. 3:11 This concatenation assignment operator appends or 3:15 adds the argument on the right side to the variable on the left side. 3:19 This can make multiple lines easier to read because 3:24 each line is a complete statement. 3:27 You finish each line with a semicolon, and 3:30 then you add to the variable, string one, 3:35 dot equals, Hello, end with a semicolon, 3:40 and again, string one, 3:45 dot equals name, end with a semicolon. 3:48 String one, dot equals, 3:56 end with a semicolon, and our last line. 3:59 Now let's run this script again. 4:10 Same thing. 4:13 This is the same as saying string one equals string 4:15 one concatenated to this next string, 4:19 because it appends or adds the new value to the end of the string. 4:24 If I wanted to add a string to the beginning instead of the end, 4:30 I could do just like this line, but instead, 4:35 I add to the beginning, I am, in the concatenation. 4:39 Now let's run this script. 4:47 I am learning to display Hello Alena to the screen. 4:50 Besides making multiple lines easier to read, 4:54 making each line a separate statement will allow you to add other 4:57 code between the lines, such as adding a comment. 5:01 Prepend to a string. 5:07 To summarize concatenation, if you want to add a single variable to a string, 5:12 you can include that variable within your double quoted assignment. 5:17 If you are concatenating a single line, 5:21 you would use a single dot between each string. 5:24 If you are concatenating multiple lines, 5:28 you should use the dot equals assignment operator. 5:30
You need to sign up for Treehouse in order to download course files.Sign up