‫Okay.
‫So we've established that PHP has different data types.
‫And in this lesson I want to focus on strings and something called string concatenation.
‫And this is a fancy way of saying that we can join strings together.
‫And I'm also going to show you a big difference between single and double quotes.
‫For now we're going to focus on single quotes, which is what I prefer to use.
‫It's completely up to you on what you want to use when you're dealing with strings.
‫My main language that I that I work with is JavaScript, and I always use single quotes there as well,
‫so I've become kind of accustomed to it.
‫But I'll show you the difference between the two when it comes to concatenation and something called
‫variable interpolation.
‫So what I have here is just basically our template.
‫I took out the the course description.
‫So basically we just have an empty card for anything we want to output.
‫And you don't have to use this template.
‫I just think it looks a little better.
‫Let me close that sidebar up.
‫So we're going to start off up here.
‫Let's open up some PHP tags.
‫And I'm going to create a couple variables.
‫Let's say first name.
‫And we'll set that to John and we'll set last name.
‫To Doe.
‫So last name and I'm using CamelCase for my variables, and I want to create a third variable that will
‫hold the full name.
‫So you might think I can do something like this.
‫Full name equals and we want the first name and then we want the last name.
‫Right.
‫And you can see I already have an error in my text editor.
‫Unexpected variable name.
‫So I'm going to reload this.
‫And I get the same I get unexpected variable last name.
‫So the reason is because we can't join variables or strings together with nothing, with just a space
‫in between, as if it were a regular text.
‫So we use something called concatenation.
‫Now, if you're coming from JavaScript then you probably would do something like this, right?
‫The plus sign will concatenate strings together, but that's not going to work here.
‫So in PHP concatenation works very similar to JavaScript but it's the period.
‫So if I do that and reload then it works.
‫And why don't we output this down here so we can see what it's giving us.
‫So I'm going to say PHP actually I'll just use the shorthand syntax and let's output.
‫Full name.
‫Okay.
‫So we'll see that that gives us John Doe.
‫So it takes the two variables and put them together.
‫Now you probably want a space in between the two.
‫So what you would do is then concatenate onto this a space.
‫So we're using a single quotes here with a space.
‫And then we need to go back into the variable.
‫So we're going to add another period for concatenation.
‫And now we have John Space Doe.
‫Okay, now you might want to insert a string variable into another string.
‫So let's say down here we want to output.
‫Let's say hello.
‫My name is and then full name.
‫So what we would do here is end the string and then concatenate on that variable and save that and reload.
‫And we get.
‫Hello my name is John Doe.
‫All right.
‫So that's and then let's say I wanted to add a line break after this.
‫So I could then concatenate go back into string and put a line break like that.
‫And that will work.
‫So you just kind of if you're putting variables in a string that has single quotes, you just have to
‫kind of concatenate in and out.
‫Now if you're using double quotes, you can use something called variable interpolation.
‫And you don't have to do it this way.
‫It's actually easier.
‫However, I for some weird reason, I kind of prefer to do it this way.
‫But let's go right under this and we'll open up another.
‫PHP tag and.
‫Let's say I want to do the same thing.
‫Hello, my name is John Doe, but with double quotes so I can actually do.
‫Hello, my name is and I can put the variable right in here.
‫So let's see what happens if I do that we get the same thing.
‫Now if I were to change this to single quotes.
‫You'll see if I reload.
‫It doesn't give me an error, but it doesn't parse the variable right.
‫It's just looking at it as as text.
‫So we'll put that back to double.
‫And another thing that I want to mention is that doing it this way with, with concatenating and using
‫single quotes is slightly faster than doing it this way with double quotes, because this is treated
‫as a literal string where this uses variable interpolation.
‫Now the difference is negligible.
‫You're most likely not going to notice it, but it is there.
‫So it's something I want to mention now real quick I just want to talk about escaping characters.
‫So if you're using single quotes I'm actually going just going to copy this one down and let's get rid
‫of the line break and even the variable.
‫So I'll just say hello, my name is John.
‫Now let's say for some reason I want to put single quotes around the name John.
‫So if I put a quote here and then I put another one here, that's not going to work.
‫In fact, if I go ahead and save and reload, we're going to get an error.
‫And the reason this happens is because PHP thinks that we're ending the string here because we started
‫here.
‫That's the opening single quote.
‫And then we have the ending single quote, and then it goes on and it doesn't know what the heck to
‫do with this.
‫So if you do, I mean, one solution obviously would be to use double quotes around the entire thing
‫like that.
‫So if I do that, that that'll work.
‫But there might you might want to use single quotes and use single quotes again inside the string.
‫So what you can do is just escape these with a backslash.
‫So if I do that and then I go to this quote here, put another backslash.
‫What this is saying is don't count this as an actual quote that's going to wrap our string.
‫Just look at it as a literal quote.
‫Same thing with this one.
‫So now if I save that and I reload now you'll see the same thing.
‫My name is John and it's wrapped in single quotes and same thing with double.
‫If you have double, like if we have all of these as double quotes, reload.
‫Uh oh.
‫We need another one there.
‫So now we see.
‫Hello, my name is John with double quotes, even though we're wrapping the entire string in double
‫quotes.
‫All right.
‫So I just wanted to to kind of talk about that for a little bit, because string concatenation is something
‫you'll do quite a bit.
‫You're putting variables inside of strings and so on.
‫All right.
‫So the next video I want to talk about typecasting and type juggling.
‫So basically having a variable change from one type to another whether that's implicit or you're purposely
‫doing it explicitly.