- When working with a program and/or scripting language like PHP, it's a good idea to add comments to your scripts to remind you of what the code does. When you come to revise a script after several months, it's a lot easier to read a comment than to study every line of code. The other main use of comments is to disable a section of code temporarily. As well as looking at comments in this video, I'll explain briefly how PHP treats white space in code. PHP has three commenting styles, two for single line-comments, and one for multiple-line comments.
One way to create the single-line comment is to begin with two forward slashes, everything after those slashes is treated as a comment that's ignored by PHP. The other style of single-line comment begins with a hash or pound symbol, so everything after that is also treated as a comment. Because they are single-line comments, as soon as you move on to a new llne, you can start typing ordinary PHP code. So let's create a variable to store my first name.
Then if I type two forward slashes after the semi-colon, everything to the end of the line is also treated as a comment, so I can explain what this code is for. And if we create another variable for my last name, and then add a hash, everything after that is also treated as a comment to the end of the line. Now, quite obviously, to create comments to explain what first name and last name are for is a little bit nonsensical, but I hope you get the idea that when you're working with more complicated code, that this can be extremely useful to remind you of what a particular piece of code is for.
So those are single-line comments. PHP also has multiple-line comments and they use the same syntax as CSS, so we begin with a forward slash, an asterisk, then the body of the comment, and you finish off with an asterisk and a forward slash. So those are the three types of comment styles that you can use in PHP. As well as remind you of what your code is for, comments can also be extremely useful for temporarily disabling part of your code without the need to delete it.
So let's just use echo down here for first name, and on a separate line we'll have a literal space and then on yet another line we'll have last name, and if I save that and load this into a browser, it displays my name with a space in between. Now that's obviously not the most efficient way to display my name, but I've done it that way to explain how we can comment out these various lines.
So on line 14, I can get rid of that space, disable the line by putting in two forward slashes at the beginning, and my code editor has already turned that into a comment. It's changed the color, it's showing me that it's disabled, and at the beginning of line 15, if I insert a hash, the same happens. Let's save that, reload the page in the browser. Those two lines, lines 14 and 15, have been temporarily disabled, and all that's displayed is the value of first name.
Now I needn't have used two separate single-line comments, I could have used a multiple-line comment, so let's just take out that hash and those forward slashes, and then up on line 13 I'm going to create a multiple-line comment, so forward slash, asterisk, and then at the end of line 14 I'll have asterisk, forward slash. That's disabled lines 13 and 14, line 15 is now enabled.
If I save that and refresh the page in the browser, it displays only the value of last name. So that's how you temporarily disable code using the various types of commenting in PHP. So let's take a quick look at how PHP handles white space. I like to have white space around the assignment operator, because I think it makes the code easier to read. But PHP doesn't worry, as long as the code is not ambiguous, you can remove white space.
- Naming variables
- Storing text as strings
- Doing calculations with PHP
- Using conditional statements to make decisions
- Creating custom functions
- Deciphering error messages
- Emailing the contents of an online form
- Dealing with multiple-choice form fields