PHP Tags and Echo0:00
Alright, at this point, you've learned how to successfully echo out a Hello World string. But yeah, what we've done here, well, right now, you'd be forgiven for thinking, but why? What's better about this versus what we had at the beginning, where we simply wrote Hello World as the content of the <h1> tag? And of course, you're right. At the moment, we're just getting used to PHP. We're shaking hands. And if you think about it, we've already learned a handful of important things. First, we can open a PHP tag like this, and then we close it at the bottom. Anything between that opening and closing tag will be interpreted as not HTML, but PHP.
First, we can open a php tag like this, and then we close it at the bottom. Anything between that opening and closing tag will be interpreted as not HTML, but PHP. So we've also learned that when we work with PHP, we can mix it within any HTML document, and that's somewhat unique to PHP. Next, we've learned that if we want to prepare a string and display it on the page, we can use echo, and then we place the string within quotes. Now, those quotes could be double quotes or they could be single quotes. We can talk about the differences later. But then lastly, we've learned that we end a command with a semicolon. Think of this almost like a period for writing English.
String Concatenation Operator1:38
For example, what if instead of world, we wanted it to be somewhat dynamic? Maybe it could be universe, maybe it could be town, maybe it could be folks, you know, whatever you want. So let's remove that. And yeah, in many languages, we use the + symbol. We would do things like this, hello + world. But yeah, you'll notice in PHP, it doesn't like that, wrong string concatenation operator. In PHP, we use the .. So yeah, this is the term we use, concatenation. We are going to concatenate hello with world.
So yeah, this is the term we use, concatenation. We are going to concatenate hello with world. Okay, let's leave it like that and see what we get in the browser. So I'll switch to the terminal. If your server isn't still running, let's boot it up like we did in the last episode and then view it in the browser. And yeah, we get the exact same thing. But now we have a new piece of syntax that we can work with, the concatenation operator. So now we can swap this out on the fly, universe, and we get hello, universe. Maybe hello, everybody, switch back, and that works as well.
Creating Variables2:32
So now we can swap this out on the fly, universe, and we get hello, universe. Maybe hello, everybody, switch back, and that works as well. Okay, cool. Next, let's make the greeting itself dynamic. Maybe instead of hello, it's hi, or what's up, or anything else you can think of. All right, let's do it a little bit differently though. This time, we're going to create our first variable. We create a variable by using the $ sign and then any number of characters. So if I really wanted to, what we have here could be a variable name. It's not a very good one, but it would work.
So if I really wanted to, what we have here could be a variable name. It's not a very good one, but it would work. But in our case, I did say the word greeting, right? So I think that's a good name for the variable. $greeting equals what? Well, in our case, why don't we make it equal to hello? So I will say, hello. Okay, and I think I'm done. But what do I put at the end again? What is the punctuation for a piece of logic?
But what do I put at the end again? What is the punctuation for a piece of logic? Once again, it's a ;. And again, in your head, just think of it sort of like a period. I am finished with this piece of logic, so I conclude it with a ;. Okay, so now I have a variable called greeting that is equal to the string hello. But if I switch back to the browser, there's no difference here because I haven't used that variable anywhere. Okay, let's swap it out. So now, let's select everything here, and I will replace it with our variable name.
Okay, let's swap it out. So now, let's select everything here, and I will replace it with our variableName. And let's see what we get there. It'll be close, but not quite. Yeah, notice I get helloEverybody as one long string or as one long word. And that would make sense if you think about it. Right now, I say echo out greeting, and then no space, and then everybody immediately after it. So we could do this in a couple ways. Of course, I could just do a space right here.
So we could do this in a couple ways. Of course, I could just do a space right here. And yeah, that would work. But maybe everybody will be dynamic as well, or that will become a variable. So another option would be to concatenate a space and then concatenate the rest of the string. So now, I'm echoing out whatever greeting happens to be, and then a space, and then everybody. Switch back, give it a refresh, and yeah, we get the exact same thing. So now, you've learned about variables.
Why Variables Matter4:39
Switch back, give it a refresh, and yeah, we get the exact same thing. So now, you've learned about variables. But you know what? When I originally learned this, I think my first thought was, once again, but why? Why create the variable? Why is that better than echoing out the hello string like we had in the last video? And the answer is, sometimes, the variable will point to things that you don't have control over. It might refer to something the user types in.
will point to things that you don't have control over. It might refer to something the user types in. It might refer to something that comes from the database. It might refer to something that you're going to manipulate in some way. Maybe the user provides a string of characters, and then you change the capitalization, or you tweak them, or you do something that you couldn't do manually or statically. That is the reason for a variable. And trust me, if that's confusing, it won't be confusing in a week or so. All right, so now, to finish up, as it turns out,
Refactoring With Quotes5:27
And trust me, if that's confusing, it won't be confusing in a week or so. All right, so now, to finish up, as it turns out, there's actually a couple different ways we could write this. And I think you'll find this is true for programming in general. Often, for any given task, there's a handful of ways you could approach it. And often, each of those ways will generate the exact same thing in the browser. So there's no difference for the end user. But for you, the programmer, you could write it in a number of ways. For example, instead of using concatenation, like we've done here,
But for you, the programmer, you could write it in a number of ways. For example, instead of using concatenation, like we've done here, another option is to store the whole thing within double quotes, like this. Greeting, space, everybody. OK, now, if I come back and refresh, yeah, once again, to the end user, there's no difference. But for you, the programmer, you've changed the code slightly. And this is referred to as refactoring. That is when we tweak the code without actually changing the end result for the user.
That is when we tweak the code without actually changing the end result for the user. We've tweaked it to be more accommodating to what you consider to be clean, or attractive, or appropriate, or efficient, or fill in the blank. All right? So now we've learned something new here. We've learned that even if we have a string, we could store and evaluate a variable within it. But now here's the catch.
Single vs Double Quotes6:41
we could store and evaluate a variable within it. But now here's the catch. If we instead use single quotes, did you notice how my syntax highlighting changed? That was a little hint. Now, we don't get what greeting evaluates to. We get the text itself. OK, so now we can see there is a difference between using single quotes versus double quotes. And that difference is, if you want to nest and evaluate
single quotes versus double quotes. And that difference is, if you want to nest and evaluate a variable within a string, you have to use double quotes. Switch back, refresh, and now it works. All right, good job. Onward to the next lesson.
