Locked lesson.
About this lesson
Understanding concatenation and concatenating assignment operators.
Exercise files
Download this lesson’s related exercise files.
String Operators.docx58.8 KB String Operators - Solution.docx
59 KB
Quick reference
String Operators
String operators allow you to concatenate strings together.
When to use
Any time you want to chain together strings, use the (.) string operator.
Instructions
When we talk about string operators, we're usually talking about concatenation using the (.) period.
<?php
$first = "John";
$last = "Elder";
echo $first . $last;
?>
You can also concatenate and assign using:
.=
Hints & tips
- concatenate with a period
- concatenate and assign with .=
- 00:04 In the last video, we talked about logic operators.
- 00:07 In this video, I wanna talk about string operators.
- 00:10 And this is the last batch of operators that we're gonna deal with.
- 00:12 After this, we're gonna get into conditional statements.
- 00:15 But string operators,
- 00:16 pretty important and basically we're looking at two things here.
- 00:19 Well, basically one thing, and the thing we're looking at is concatenation.
- 00:24 That's concatenation, and
- 00:28 let's just erase this and start over and let's go first name, last.
- 00:33 Create two more variables and
- 00:34 I'm gonna add John to the first name or first variable.
- 00:39 And for the last, type in elder.
- 00:42 Let's say we wanted to put these two together.
- 00:44 We had math operator that would add numbers together.
- 00:48 But let's go first + last and see what happens.
- 00:54 If we wanna smush these two together and
- 00:56 make John Elder, some programming languages let you do addition.
- 01:01 PHP is not one of them.
- 01:02 Math operators are out, what do we do?
- 01:04 We use a string operator which is a period.
- 01:08 We save this, hit reload, and we get JohnElder.
- 01:11 You noticed, there's no space in between.
- 01:13 We could put a space by creating a string with a space, and
- 01:16 then another period if we did that, but we get space there, but
- 01:20 concatenation is very important, that's the first string operator.
- 01:25 There's also this sort of, it looks like assignment operator and
- 01:28 that's in fact what we're doing, we're concatenating and assigning this to that.
- 01:32 So if we save these, it looks it's the same thing but
- 01:35 that's not actually what's going on here.
- 01:38 If we echoed out, let's create a line break in PHP
- 01:43 like we did before and then echo out first.
- 01:47 What we're doing here is we're concatenating, smooshing them together,
- 01:51 and assigning.
- 01:53 So first, changes from John to John Elder,
- 01:56 because that's what the concatenation assignment operator does.
- 02:00 Save this, hit reload, we see boom, John Elder.
- 02:03 On the other hand, if we just do a regular concatenation, if we hit
- 02:08 reload, the first line, it's smooshing them together and echoing it out but
- 02:13 it's not changing the variable, it's not changing our first variable.
- 02:17 That continues to be John, and so the second line, it just prints out John.
- 02:21 Depending on what you wanna do, if you just wanna smoosh two things together,
- 02:25 just use your regular concatenation operator.
- 02:27 If you want to smooch two things together and then save the them
- 02:31 into the first variable, use your assignment operatory looking thing there.
- 02:36 Save this, come back, hit reload, boom, John Elder.
- 02:38 You can always add stuff.
- 02:43 And concatenate until your heart's content.
- 02:46 And that is actually very, very useful.
- 02:49 You'll use this concatenation thing all the time.
- 02:51 Anyway, that string operators, very useful and now we are done with operators.
- 02:56 We went through arithmetic operators, assignment, comparison, increment,
- 02:59 logic, and now string.
- 03:01 In the next video, we're gonna start talking about conditional statements,
- 03:04 if statements basically, if else statements.
- 03:06 A lot of fun. You could do a lot of stuff with them and
- 03:08 we're gonna start to add back in the stuff we've already learned,
- 03:12 specifically the comparison operators and the logic operators.
- 03:15 And you'll finally get to see them used in actual real life examples,
- 03:18 starting in the next video.
Lesson notes are only available for subscribers.