Starting PHP Server 0:00Now, in the last lesson, you actually learned more than you might think. You learned how to create a PHP file, trigger it from the command line, and then you also learned a bit about strings and how you're supposed to end any command or operation with a semicolon. So now let's move on to variables. But before we do that, yeah, up until this point, you've been doing it through the terminal. But you might be thinking, well, Jeff, I want to make a website, so how do I see this in the browser? Fair enough. Let's boot up a little server.Fair enough. Let's boot up a little server. Now if I run php-h, it'll spit out a lot of help here. These are just various little options you can run from the command line. I'm going to search for server. Okay, so notice if I add dash capital S and then the address, I can run a built-in server. Cool. Let's try it. php-s, and we're going to call this localhost colon, and we want port 8888. Really any port would be fine.php-s, and we're going to call this localhost colon, and we want port 8888. Really any port would be fine. But just stick with this for now. Okay, so using php7, we are now listening at this URL. So you can copy this and paste it into Chrome or Firefox, or if you are using iTerm, you can command click any link here. Okay, so now the browser pops up, and we can see the web representation of that code you wrote. Cool. I know that's a little more fun than doing it in the console. Creating Variables 1:23Cool. I know that's a little more fun than doing it in the console. So no problem. All right, so let's go back to Sublime Text. The next step is variables, and variables are a confusing thing at first. I remember when I was first learning, I would think to myself, well, why would I ever use a variable? Why would that be better than just referencing the value directly? And as you'll find, there's lots of reasons to do it. So first step, how do we create a variable?And as you'll find, there's lots of reasons to do it. So first step, how do we create a variable? We do it by adding the dollar sign and then any string. We do it by typing dollar sign and then whatever name we want. So imagine you want a variable for this right here. What is hello world? It is a message, or it's a greeting. All right, let's call it greeting, and then we make that variable equal to, once again, anything we want. There's lots of different constructs I'll teach you about.anything we want. There's lots of different constructs I'll teach you about. But for now, you just know about a string. So we're going to paste that in right here. And once again, we're done with our little operation, so we end it with a semicolon. Okay, well, that's your first variable. Good job. So now if you want to echo that out, we can replace all of this entirely with the name of the variable, greeting. Okay, so save it with Control or Command S. Switch back to Chrome.of the variable, greeting. Okay, so save it with Control or Command S. Switch back to Chrome. And if I give this a refresh, you're going to see the exact same thing, but now you're using a variable. Great job. So imagine you want to change this to hello universe. Okay. You update the variable, and it will automatically be reflected, of course, down here. And by the way, often on a Mac, you can hit Command Tab, just real quick, just a quick swipe there, to switch between the two most recently used applications.And by the way, often on a Mac, you can hit Command Tab, just real quick, just a quick swipe there, to switch between the two most recently used applications. So you'll see me do that quite a bit. And I'm hitting Command Tab each time. Okay, anyways, if I give this a refresh, or I can hit Command R to do the exact same thing without hitting that little button there. Anyways, we see it updated here. Now still though, you might be thinking, well, I don't get it. Why is this better than what we had before? And the answer is, in this case, it doesn't make one lick of difference.Why is this better than what we had before? And the answer is, in this case, it doesn't make one lick of difference. And that's because we created the variable, we did modify it, and we echoed it out immediately. So as it turns out, this example is a bit superfluous. But what you will find is that in real life, you will be modifying these values all of the time. For example, imagine you have your name. Okay, create a variable. Don't forget, always use the dollar sign at the beginning. That's how we describe that this is a variable. Interpolating in Strings 3:57Don't forget, always use the dollar sign at the beginning. That's how we describe that this is a variable. Okay, make it equal to your own name, Jeffrey Wei. Next, we're going to echo, hello, but then I also want to say, hello, and then the name of the variable. Okay, well, you can't do it like this. If we try it, we're going to see the variable name rather than the value associated with it. And if you remember, one or two lessons ago, I noted that there's a tiny difference between single and double quotes.And if you remember, one or two lessons ago, I noted that there's a tiny difference between single and double quotes. So that difference is, with double quotes, you can use variables. So we can interpolate things like this automatically, but only if you have a double quote. So if I refresh it, now you'll see, hello, Jeffrey Wei. But with single quotes, yeah, you can't do that. Now, here's another way you can do this. You can use concatenation. So I can use a period here and then the name of the variable. And you know what? Concatenating with Period 4:52So I can use a period here and then the name of the variable. And you know what? Period's kind of confusing. A lot of other languages will use something more sensible, like a plus. But in our case, yeah, PHP wants us to use a period. So think of this as saying, I want to echo out to the screen, hello, comma, space, and then concatenate or add to it the value of this name variable. And that's how we get the same thing here. But in this case, yeah, I often will do something like this, hello, name. Or you also might see folks around the web surround it with braces.But in this case, yeah, I often will do something like this, hello, name. Or you also might see folks around the web surround it with braces. And really, it's optional here. But a lot of people do this just to make it that much more clear that they are referencing a variable. Let's keep it simple for now, though. OK, so think about the flexibility you have now. Name, this name variable, could be defined anywhere. It could even be defined in a little text input on the page, where you go to the website and it says, what is your name?It could even be defined in a little text input on the page, where you go to the website and it says, what is your name? So you type in your name, John Doe or Jane Doe. And then when you click a button, it says, hello, John or hello, Jane. So that value that you type in can be saved to a variable. And it can correspond to your name or my name or anything that you pass in. So if we change this to Jane Doe, now this will automatically be updated to reflect that. OK, so that'll do it for this lesson. Now if you're anything, once again, if you're like me, variables took a little while to sink in and really understand why I would use them. Homework: Practice Variables 6:19Now if you're anything, once again, if you're like me, variables took a little while to sink in and really understand why I would use them. So if you're feeling that way, you're totally normal, no problem there at all, just stick with it. Your homework for this lesson is, keep playing around with this exact thing. Create a variable and then find some way to echo it onto the screen. And if you want, have some fun with it. Create multiple variables, such as greeting, and maybe that could be hello. Then you could say echo greeting, and then we will concatenate a string and then concatenate the name.Then you could say echo greeting, and then we will concatenate a string and then concatenate the name. Now we are constructing a full sentence using two different variables here. Now if we refresh it, we get the same thing. Or like I said, you can nest it within a string, only if it's double quotes. So greeting, name. And that will get you the same thing here. Or like I said, lots of people will often wrap it, especially with things like this, where you have commas and you just want to separate the variable a bit more. You could do something like this as well.where you have commas and you just want to separate the variable a bit more. You could do something like this as well. Refresh, and of course you'll still get the same thing. Okay, so keep playing around with this, and once again, when you're finished, we'll move on to step four.