Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie, we're going to learn how to insert code comments into our PHP. We don't want to become simply capable developers, we want to become good developers. And in order to do that, we need to learn about code comments, something that's very important in any programming language. You want to add comments to your code so that you or someone else coming to look at the code later. Can quickly understand what your code is trying to accomplish, and understand the approach that you took. Comments take additional time to write, but they pay off and save you time later. In PHP, there are a couple of ways we can write comments.
We can make single line comments by simply opening PHP tags, and then putting in // followed by our comment, single Line comments are like this. Or we could use a pound sign. Or like this but that is less common. Mostly you're going to see people using the slash slash in front of it and that's what I'll be using throughout this title. Now lets just save that document real quick and lets go over here and lets reload this hello world page in our browser.
So there's the page, let's do view source on it, web developer, and page source. And you can see the comments do not show up, right, just the things that I echoed out, just the output. So these comments are not output into our HTML. And they are not processed as PHP, they're just simply ignored. They're there for our benefit only. And I want you to note two things about them. First of all, this only works inside PHP tags. right? If we did it up here, then that's just html that we're writing, that's just text that will be output, so we can't put them there.
And also this technique doesn't work for double-line. So for example, if we have double-line comments that keep going, oh wait a minute. Notice that TextMate actually helpfully colored it a different color for me. The brown text color indicates that it's a comment. The blue is indicating that it's going to be, try and process this as some kind of PHP. And I'm going to get an error. Now, we could just put more // in front of it. That's certainly valid to do your comments that way. But I also want to show you that you can do double-line comments by putting A slash with an asterisk after it.
So double line comments are written like this, so that you can keep typing. I'll just indent a little bit and typing. And you can do that all the way until you type another asterisk, and a forward slash. So everything in between these, is all going to commented out. It's a little bit like turning on and off the PHP tags for PHP processing. We're saying this whole thing is a comment until you get to the ending comment marker just assume that we're still in comment mode.
And this is great because it allows us to type entire paragraphs describing what our code does. And of course one thing I want to a caution you about is you don't want to try to put a multi line comment inside another comment because as soon as you get to the first ending slash, alright, even if we have another starting slash, test. Right it's going to close it and turn off commenting mode, they're not nested, so two turn ons does not equal two turns offs, right, so as soon as we get to an off that's it, it's out of comment mode, and let's save that and then let's just comeback over here and reload the page one more time and take a look.
View Source. And you can see that it doesn't show up there either. Comments are super important to writing good code. They can be hard to be disciplined about it. You get caught up in writing the code, and often you forget to leave yourself comments. But once you start programming a lot you'll realize that comments are going to save you a lot of time, not now, but 10 weeks from now. When you come back to the code. You can't remember what you were trying to do or why you chose a certain approach. It's one of those things where investing a little bit of time in the beginning is going to save you lots of time later on. And comments are especially friendly if there's ever someone else who's going to be working with your code, because they don't know what you were thinking. They don't know The reasoning that you went through to arrive at a solution to a certain problem, but your comments can make it clear to them. So I'd like you to make a promise to yourself right now that you're going to try and put as many comments in your code as you can. Especially while you're still learning PHP.
Get unlimited access to all courses for just $25/month.Become a member