WEBVTT

1
00:00:00.420 --> 00:00:04.320
Combining data in a string
is called concatenation.

2
00:00:04.320 --> 00:00:07.070
We perform concatenation in the last video

3
00:00:07.070 --> 00:00:10.900
by adding one string variable
to another string value.

4
00:00:10.900 --> 00:00:14.060
By using double quotes,
we included the first variable

5
00:00:14.060 --> 00:00:16.605
into the string being assigned
to the second variable.

6
00:00:16.605 --> 00:00:22.950
PHP also provides two string operators for
concatenation.

7
00:00:22.950 --> 00:00:25.230
The dot and the dot equals.

8
00:00:25.230 --> 00:00:29.350
Let's take a look at how to use these
operators for more concatenation options.

9
00:00:30.680 --> 00:00:35.640
The first string operator for
concatenation is a single dot, or period.

10
00:00:35.640 --> 00:00:40.200
The dot allows us to add as many strings
together as we want in one statement.

11
00:00:40.200 --> 00:00:44.190
For example, let me duplicate this line,
then I can show you how to do

12
00:00:44.190 --> 00:00:49.350
this same thing using single quotes, but
still show the value of the variable.

13
00:00:49.350 --> 00:00:54.381
We end this string here and
concatenate with a single dot,

14
00:00:54.381 --> 00:00:58.705
concatenate again, and
start our final string.

15
00:01:03.070 --> 00:01:06.780
Our string works, and
our variable value is displayed.

16
00:01:06.780 --> 00:01:09.300
But our escape sequences
aren't working properly.

17
00:01:10.520 --> 00:01:15.994
We can remove the escape character
from before the double quotes, and

18
00:01:15.994 --> 00:01:21.400
then we can concatenate this escape
sequence, the /n, to the end.

19
00:01:28.565 --> 00:01:31.530
Now when we run our script,
just like we want.

20
00:01:32.670 --> 00:01:35.920
Remember, PHP doesn't care
about extra white spaces.

21
00:01:35.920 --> 00:01:38.912
So as long as you haven't finished
your statement with a semicolon,

22
00:01:38.912 --> 00:01:40.832
you can split this line
up however you want.

23
00:01:50.890 --> 00:01:53.845
Be aware that if you add a line
break within the quotes,

24
00:01:53.845 --> 00:01:56.130
that line break will be displayed.

25
00:01:56.130 --> 00:01:59.860
This could be a good or bad thing,
depending on the desired results.

26
00:02:01.440 --> 00:02:05.110
Early on I said, with great power
comes great responsibility.

27
00:02:05.110 --> 00:02:09.050
So just because you can concatenate
your string over multiple lines

28
00:02:09.050 --> 00:02:12.070
doesn't mean that you should,
splitting up a statement

29
00:02:12.070 --> 00:02:15.480
into multiple lines can make
it extremely hard to follow.

30
00:02:15.480 --> 00:02:20.430
If you are going to split the line, try
to make it as easy to read as possible.

31
00:02:20.430 --> 00:02:24.143
Indent the next lines and start each
line with a concatenation character.

32
00:02:39.729 --> 00:02:42.450
Notice that extra space
right before Hello Alena?

33
00:02:43.590 --> 00:02:49.540
That's because our string is split between
two lines, and it's within single quotes.

34
00:02:49.540 --> 00:02:53.400
So it's trying to display the characters
exactly as they're shown.

35
00:02:53.400 --> 00:03:00.350
I could end my string here, and
then start my string again down here.

36
00:03:03.130 --> 00:03:04.050
Now we have a single line.

37
00:03:05.290 --> 00:03:07.230
Now this is easier to read, but

38
00:03:07.230 --> 00:03:11.150
it's probably not the best choice if
you're going to use multiple lines.

39
00:03:11.150 --> 00:03:15.990
You're probably better off using
the second operator, the dot equals.

40
00:03:15.990 --> 00:03:19.880
This concatenation assignment
operator appends or

41
00:03:19.880 --> 00:03:24.440
adds the argument on the right side
to the variable on the left side.

42
00:03:24.440 --> 00:03:27.350
This can make multiple lines
easier to read because

43
00:03:27.350 --> 00:03:30.150
each line is a complete statement.

44
00:03:30.150 --> 00:03:35.262
You finish each line with a semicolon, and

45
00:03:35.262 --> 00:03:40.374
then you add to the variable, string one,

46
00:03:40.374 --> 00:03:45.628
dot equals, Hello, end with a semicolon,

47
00:03:45.628 --> 00:03:48.610
and again, string one,

48
00:03:48.610 --> 00:03:53.444
dot equals name, end with a semicolon.

49
00:03:56.371 --> 00:03:59.545
String one, dot equals,

50
00:03:59.545 --> 00:04:04.598
end with a semicolon, and our last line.

51
00:04:10.673 --> 00:04:12.050
Now let's run this script again.

52
00:04:13.690 --> 00:04:15.200
Same thing.

53
00:04:15.200 --> 00:04:19.895
This is the same as saying
string one equals string

54
00:04:19.895 --> 00:04:23.360
one concatenated to this next string,

55
00:04:24.420 --> 00:04:30.280
because it appends or adds the new
value to the end of the string.

56
00:04:30.280 --> 00:04:35.460
If I wanted to add a string to
the beginning instead of the end,

57
00:04:35.460 --> 00:04:39.247
I could do just like this line,
but instead,

58
00:04:39.247 --> 00:04:43.749
I add to the beginning,
I am, in the concatenation.

59
00:04:47.021 --> 00:04:48.090
Now let's run this script.

60
00:04:50.230 --> 00:04:54.118
I am learning to display
Hello Alena to the screen.

61
00:04:54.118 --> 00:04:57.103
Besides making multiple
lines easier to read,

62
00:04:57.103 --> 00:05:01.131
making each line a separate statement
will allow you to add other

63
00:05:01.131 --> 00:05:04.355
code between the lines,
such as adding a comment.

64
00:05:07.689 --> 00:05:10.270
Prepend to a string.

65
00:05:12.600 --> 00:05:17.490
To summarize concatenation, if you want
to add a single variable to a string,

66
00:05:17.490 --> 00:05:20.810
you can include that variable within
your double quoted assignment.

67
00:05:21.820 --> 00:05:24.560
If you are concatenating a single line,

68
00:05:24.560 --> 00:05:28.000
you would use a single
dot between each string.

69
00:05:28.000 --> 00:05:30.860
If you are concatenating multiple lines,

70
00:05:30.860 --> 00:05:33.840
you should use the dot
equals assignment operator.
