PHP Basics

Strings

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
PHP Basics

Check out a free preview of the full PHP Basics course

The "Strings" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano highlights the difference between single, double, and multi-line use cases when creating String values in PHP. Strings can be concatenated with the ".", not the "+" like in other languages. Strings using double quotes support variable interpolation.

Preview
Close

Transcript from the "Strings" Lesson

[00:00:00]
>> Maximiliano Firtman: So let's talk a little bit about the strings. That is something that we are going to deal a lot. First, remember, we create a PHP file. The next first step, what is, opening the tag. If you don't do that, you don't have a PHP file. I mean, you have a PHP file, but everything you're writing will be just literal output, okay?

[00:00:20]
Okay, so strings, so you already thought that I can just create the title of this course is PHP fundamentals. Okay, so we just assign a string, but double quotes, single quotes, is that the same? Well, let's dig into that, we have single quotes.
>> Maximiliano Firtman: Okay? That works as well, and double quotes, but they're not exactly the same, why?

[00:00:50]
I can for example say that, I will create a message here, and I can say, the welcome to, and I wanna concatenate with the name of the title. Okay, so how do you concatenate? Well, I'm pretty sure that you're expecting this, right? So you say +title. But this is PHP, so the answer sometimes is not so simple.

[00:01:21]
We don't have the plus operator for concatenation in PHP.
>> Maximiliano Firtman: Why did you say no, no, yeah, I'm feeling your pain, so you need to get used to this, yeah, I take some time. I remember myself when I was switching languages from JavaScript to PHP that I was forgetting about that and I said, what is the operator?

[00:01:46]
Dot, we concatenate the strings with the dot, okay, it takes time to get used to, but it's not plus it's dot. Actually, it's not the only language that is not using the plus for concatenation, there were others, Objective-C, and others where it was different. But yeah, now all the languages try to use the plus, okay?

[00:02:14]
Remember that it's not actually concatenation does not really exist, it's actually created in a third string, a new string. But instead of doing that, and you will like this one, instead of doing that, I can also create another version of the message. So message2, so maybe you want, hey, do we have a way to do something like, I mean, in JavaScript, we have this idea, another template string, where you can do the same thing.

[00:02:39]
Well, here in PHP, we can do this, let me show you first. By the way, I know it's wrong, so if you know PHP, maybe you're saying, hey, Max, that's wrong, yeah, but give me a sec. So, if I execute this, strings.php, I'm getting welcome to dollar sign, title, so, yeah, it's not working, right?

[00:03:02]
Why is it not working? Because I'm using single quotes, and that's the big difference between single and double quotes. Double quotes will let you use interpolation. Interpolation is when you have a variable or an expression used it, but it should contain a variable expression. I will get into that in a second, in a string, so now if I execute this, it says, welcome to PHP fundamentals.

[00:03:29]
So then, we don't need to use the dot all the time, [LAUGH] okay, we can just use double quotes for your strings and then just put the name of your variable in sign, and the dollar sign will say, this is the output, okay?
>> Speaker 2: So that only works with the double quotes, you're saying?

[00:03:49]
>> Maximiliano Firtman: Double quotes, yeah, only with double quotes. If you do single quotes, it will not interpolate anything inside. So the string that you put is a string that you get in the output as a literal.
>> Speaker 2: Okay, would you generally only use the double quotes then in-
>> Maximiliano Firtman: I mean, that's a first reaction.

[00:04:06]
What's the problem of always using double quotes? Maybe performance, maybe this might not be a big deal, but when you have double quotes, the interpreter expects that there might be an interpolation inside, so I need to check if there is or not an interpolation. And in single quotes, it's a literal, so it's it notes, it doesn't need to search inside, so it's about performance.

[00:04:30]
And that's why, yeah, but if you look at PHP code out there, probably no one care a lot about that, so yeah, most developers are just using double quotes and just in case, what they're not, they don't think about the performance part. Again, it's not a big performance issue, right, it's a couple of milliseconds that might take more if you have a double quote, a string, and no interpolation inside.

[00:04:55]
So do we have any other type of string? Yeah, we have two more, that will look probably all for a lot of you. So, we don't have the back tick, so if you're coming from JavaScript, no back tick, okay. No triple double quotes, some other languages as well, let go, no.

[00:05:15]
We have this, we can create a variable like it can be a, let's say a JSON. Because how can I make a JSON? Because a JSON, as a string, let's talk about string, I'm not talking about the JSON format, but as a string, right? It's typically one multiline, right?

[00:05:32]
So I wanna go multiline and I wanna do a lot of stuff inside. So, how can I do that? Well, the way to do that is using this syntax that is coming. So, the double quotes and the single quotes maybe are coming from C, Java, the JavaScript to get from that part, this one, the one you will see now, I'm not sure if you have seen that before.

[00:05:55]
So in fact, let me point you to the documentation, php.net. We have a special for strings now, but then should be inside types strings. So you have the heredoc syntax, the nowdoc syntax, is it there what? So I here is the thing case a third way to delimit a string is this syntax, so let me explain that one, you start with three less characters and a name, an identifier.

[00:06:29]
Typically, we use upper case identifiers, but it's not mandatory. We can say, we are going to write JSON, okay, but it can be anything, like PRETTYJSON, wherever, any anything. Then you can write code and for example JSON, let's take any JSON sample from the web, any weird one, doesn't matter.

[00:06:55]
And when PHP knows or expect that to end, when you put the identifier again, so for example here, I say PRETTYJSON, that's the end. So what, let's remove some lines so we can see the beginning at the end. This is how it looks like, so you start with this, with an identifier, and then when the identifier repeat somewhere in the string, it means, it's ending here.

[00:07:31]
>> Speaker 3: Does it validate that it's actually valid JSON depending on-
>> Maximiliano Firtman: No, no, that's a string. At this point, this is just a string, okay? So it's just a string. We will talk about JSON parsing later, but this is just a string, okay? And to make things even weirder, yeah, it can get weirder.

[00:07:53]
Let me remove this one, I wanna increase the font size one point. So what if I wanna take message2 and put it here? Can I interpolate like we did with double quotes before, like using dollar sign, message2? So actually, no, so if I try this, even if I try to do an echo, by the way, echo of JSON, this PRETTYJSON needs a semicolon, cuz it's ending that line, that string.

[00:08:25]
So if I try to do this, okay, I'm getting the output. Welcome to fundamentals, that's because I saved it, yeah, I didn't save. Okay, so actually, the difference between single quotes and double quotes happen here if you put the identifier in single quotes or without quotes. So that's weird, I mean, it is weird.

[00:08:55]
And we were looking at the documentation here. This is the difference, are when you go at the top, we have the heredoc is the one without the identifier. Here they are using END, but any identifier works here. And the other one is called the nowdoc. The nowdoc is the one with single quotes.

[00:09:22]
>> Maximiliano Firtman: It is just that, okay, I mean, it's a weird thing, but it's not the creation of PHP, okay, this is coming from other places. That's why PHP sometimes feels like a fusion of different languages working together.
>> Speaker 4: This is a lot like Unix shell.
>> Maximiliano Firtman: Yeah, it's coming from there, it's coming from Unix shell, it's coming from there.

[00:09:42]
In fact, it's the same name, then these names, these two names, heredoc and nowdoc, are coming from there. We use this only on some specific situations like PRETTYJSON, that we don't typically write a JSON like this, we use a parser, writing a SQL query that is multiline, a very complex SQL query.

[00:10:02]
So, those situations, okay, it's not so common, to be honest. Most of the time we use just single quotes, double quotes, and that's all. So, just as I remember, single quotes won't do interpolation, double quotes are doing interpolations, and if you wanna concatenate, it's the dot operator, not the plus.

[00:10:22]
I think those are the most important parts regarding string management. Okay, so something else that might be important here is that, you may be, if you want, for example, let's say year 2024, and you may wanna add expressions as well. So what if you want to say year +1?

[00:10:47]
What will happen here? Will it pre 2024 + 1 or 2025, well, actually, this is just going to interpolate until here, so there. And then, if you wanna change that, you can use kind of a code block here like that, and then it works like that. And then you have an expression inside.

[00:11:16]
>> Maximiliano Firtman: Okay, it's not common, to be honest, it's not common. We typically, I mean, it's common in another languages, but in PHP, we don't use that a lot.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now