Placeholder Image

Subtitles section Play video

  • So let's talk about commenting your PHP code.

  • So this is a very useful tool, especially for new coders, when many times you kind of sort of forget what you're trying to do in the first place.

  • So what commenting allows you to do, and it allows you to add comments and notations within your code that is not actually run as part of the PHP script, so you can go in.

  • And if you have a function, you can add a little notation of what that function is supposed to do and what the output is supposed to be.

  • Or, if you're doing anything else, you're setting variables, that type of thing.

  • You can add notations in to say what is supposed to happen and what you expect to have happened so that if you run into problems with your code when you run it, you can go back and very easily try to find where the problem is.

  • This is valuable as your code starts to become larger, so when you have a little little scripts that are 10 or 20 lines, is pretty easy to simply be a bit worried that code and know exactly what's going on.

  • Once your code starts to get above 100 lines, when you have hundreds of lines of code, it can be difficult to remember what each individual snippet is supposed to do.

  • And what else relies on that snippet of code within the script.

  • So one of the problems you can have is you sit there and you realize, Oh, the output that I'm getting is wrong.

  • So you go back to maybe fix a function that you created or do something.

  • You fix that.

  • And so now this output is correct.

  • But then some other output within your script is now wrong because something else in the script was calling to what you were doing.

  • And now you have problems.

  • So if you have the comments, if you have a little notations, they're you know what's supposed to be happening.

  • This especially very valuable as a new coder.

  • When you write code, if you go back a month or two months or three months later, it's amazing how you you don't remember what you're doing.

  • Everybody, everybody has this idea has I know exactly what I'm doing.

  • I can come back at any time and make any changes that I need And then it's a week or two later and you go back to that code that you wrote it kind of scratching head gone.

  • Did I write this?

  • This doesn't really look like something I wrote.

  • Uh, what am I supposed to do here?

  • So by having the comments, it just makes life a lot easier because you could go and you have a human readable.

  • Okay, this function is supposed to do this, and this is supposed to do this.

  • And this is why I set it up the way that I did.

  • It's also valuable for other coders.

  • So if you create a script that somebody else may have to maintain or edit in the future, they can go and they can see at least what you were thinking.

  • What you were trying to d'oh.

  • So they have a better idea of how to try to go in there and fix it or try to improve upon what you've done.

  • So commenting is just one of those very simple tools that makes life just a whole bunch easier.

  • So with that, let's go over to the computer circuits so I can show you how commenting is done in Ph.

  • P and show you what the output looks like.

  • So in PHP, there are two types of comments.

  • There is a multi line comment, So basically, this is where you comment out multiple lines.

  • So what this does is you start with a forward slash and then you do a star.

  • And then basically everything gets commented out.

  • So everything between that and then the star backslash is commented out and PHP will not run that code.

  • Where multi line comments really make life A lot easier is if you're trying to comment out entire snippets of code.

  • So let's say you create a function or if you're going to be doing something and you think I don't want in that to run at this particular time, what you can do is you can just simply go through, and you can just comment out all of that code.

  • You don't want to run.

  • So let's say here, if I didn't want if I didn't want from here, let's say down to here to run.

  • I could simply comment that out.

  • And then this part of the script simply won't run because it's considered commenting out.

  • So PHP says.

  • Oh, this information has commented out.

  • This should not run, so this could be useful, especially when you're trying to troubleshoot things or you're trying to build up your script.

  • You have individual components that are supposed to function and work.

  • This is a way you can comment out certain sections so that those sections don't run as you go through and figure out what's supposed to work.

  • So let's say you had a notification system.

  • So let's say information is both supposed to go into a database, and it's supposed to fire off a email in PHP.

  • Well, when you're sitting there when you're troubleshooting, you may not want the email functionality to work.

  • You may not want e mails he sent out every time you add something to the database.

  • So what you could do is you could comment out your your email function, so that is not going to run while you're doing your trouble shooting while you're doing your testing.

  • And then once they're done than Ewan, comment it and it will work.

  • So that's how the multi line comment works.

  • All you have to Dio is the forward slash star, and then everything between that and in the star backslash that will be commented out and PHP won't run.

  • The other way to comment in PHP is the single lion comment.

  • So for the single line comment here, what you're going to have is you have to forward slashes, so forward slash forward slash and then whatever comes after this on a single line that will be commented out and PHP will not run.

  • So this is normally used when you put notations within your code.

  • So you want to say what a function does or why you're setting a variable.

  • Most people would use the single line commenting in order to do that.

  • So double liar multi line commenting is usually to cut out snippets of code that you don't want to run a single line.

  • Commenting usually is used to add notations for whatever you're trying to do in your script.

  • So here what you can see is I just add a single line notation, you know, creating variables, and then I create these particular variables here.

  • And then I could do this notation saying, creating variables with bad data.

  • So if I come back to this code, if I come back for this code in two months.

  • I'm trying to figure out what the hell I was doing.

  • It was like, Oh, okay, that's what I was doing.

  • I was purposely trying to create garbage data.

  • That makes sense.

  • And then here I have a single the single life for commenting.

  • Ah, that says, you know, outputting the good variables so I can sit here and go.

  • Okay, So here what I'm trying to do is output good variables that makes sense.

  • And then here, outputting variables with bad data.

  • Okay, so this is the example that I was using for what?

  • That bad dad it would look like.

  • Now I have something that's very easy to be embassy.

  • And look at now.

  • If we go over and we take a look at the web page, it's created from this, you can see it looks looks like the garbage script that we did before.

  • And then the other Good thing is, when you go to the page source, you will notice that the comments are not there now.

  • To be clear, from a security standpoint, don't put your database information into the comments, right.

  • But you could put reminders right so you could say something like this function connects to expires The database now, to be clear in the comment for security reasons, you don't want to put what the database server name is.

  • You don't want to necessarily say some other things, but basically, you could put some information in there so that you know, again, what you were you were trying to dio um, So if you go back there to troubleshoot right, so again, be very careful with commenting.

  • But do you realize that the commenting is an output it as sore?

  • So if somebody goes back to look at the source that they're not going to be able to see that information And that's really all there is to the multi line comment in the single line comment and PHP.

  • So now that you've seen how commenting works in PHP, the main thing to remember the main thing to remember Now whenever anybody talks about commenting, you know, instructors or that we'll talk about having, you know, good code and maintainable code.

  • So when somebody comes behind you to deal with your code, don't know what's going on.

  • But let's be honest.

  • Let's be honest.

  • You should do commenting to be selfish because the reality is you're the one that's gonna be going back in dealing with your own code.

  • And if you haven't commented your own code, there's a good chance when you go back to take a look at it a month or two or six from now, you are literally going to have almost no idea what the hell that you were trying to.

  • D'oh!

  • It's one of those things, especially with new coders.

  • You sit there, you get a script to worry.

  • That's the thing is you get something to work it out puts what it's supposed to outputs, that you're all happy.

  • Yea, you set out on a server, it starts raining and starts doing its thing six months from now.

  • You think?

  • Oh, you know, an email notification system would be really great for the script or some other type of thing.

  • You then go open up your script and the script that you wrote.

  • You look at it.

  • Ah, uh, What was I doing?

  • What was I thinking?

  • And so if you have comments, There you go.

  • Oh, yeah.

  • Okay.

  • This This This right here.

  • This This is what I want need to deal with.

  • So as Faras commenting comments go, Don't think about this as being altruistic, that you're being nice to the next person to come.

  • Come and deal with your code.

  • Think of this from a selfish standpoint of when you have to go back and edit your own code.

  • It will make your life easier again.

  • Even a couple of it's amazing.

  • You write some code 34 days later, you look at the code that you wrote and you're thinking, Yeah, he hasnt supposed to do something isn't so, uh, so Make sure to comment.

  • So that's how you do commenting and Ph.

  • B again, generally multi line commenting, is used to cut out snippets of code, especially for doing things like troubleshooting.

  • Single line comments are used again, more or less for notations and for actual comments within the code.

  • And that's how it's done.

So let's talk about commenting your PHP code.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it