首页 > 代码库 > Comments and whitespace
Comments and whitespace
In this video, I‘ll show you the various ways of adding comments to PHP scripts.
小结一下几种注释形式:
HTML:<!-- --> ;
CSS: /* */ ;
JS: // , /* */ ;
PHP: # , // , /* */ ;
I‘ll also explain PHP‘s attitude to Whitespace. The most obvious reason for adding comments to a script 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. PHP has three commenting styles, two for single line comments, and one for multiple line comments. The first style for a single line comment is to begin the line with two forward slashes.
This is a comment, that will be ignored by PHP. The other type of single line comment begins the line with a hash or pound sign. Anything following the double slash or the hash sign is treated as a comment. But because they‘re single line comments, as soon as you move to another line, you can start adding commands.
This is a regular PHP statement which ends with a semi-colon. And because of the way in which single line comments work, I can add a comment at the end of the line. Just like that. And it works exactly the same with the hash and the pound sign. This shows one of the advantages of choosing a script editor that has syntax highlighting.
The comments are all shown in orange, whereas, the code is shown in its normal colors. Creating a multiple line comment uses exactly the same style as in CSS. You begin with a forward slash and an asterisk. And then you finish with an asterisk and a slash.
Obviously, when you‘re adding comments to a script, I hope that your comments are a little bit more meaningful than the ones that I‘ve used as examples here. But the other main reason for using comments is to disable part of the code temporarily without needing to delete it. (对于一些代码,确定用不到了,再删除,要不然又需要重新输入)So, let‘s just add some extra code here. I‘m going to use Echo to display the values in those variables. And then, save the page and view it in a browser.
So, that we are the values of first name and last name have been displayed. There‘s no space between David and Powers because PHP ignores Whitespace. If you want to add Whitespace in between, you need to add it as text. So, I‘m going to add another Echo command here with a string that consists of a single space. If I save that, and view it again in the browser, reload it, it‘s now added the space between those two values.
Now admittedly this isn‘t the most efficient way of adding a space between first name and last name. I did it this way because I want to be able to show you how to comment out code without needing to delete it. So, if I go in here and add a double slash there, it turns it into a comment. I‘ll add a pound sign here. That‘s also turned it into a comment. Save the page and refresh the browser. And the space and last name have been displayed, so that it only displays first name.
Rather than adding the single line comments, I could equally have used a multiple line comment to disable both of them at the same time. So, if we just save that and refresh the browser. It‘s working the same way as before. So that‘s how you create comments in PHP. But before ending this lesson, I‘d just like to point out that PHP ignores Whitespace in code, as long as it doesn‘t lead to any ambiguity.
Now, one thing that I could do is, I could remove the Whitespace around the assignment operator and that makes no difference to the way in which the code is executed. If we just check that again, it‘s still working. But what might come as a bit of a surprise to you is that, I can add extra lines and Whitespace in here. And even put the semi-colon many lines further down. And if I save that again and refresh the browser, it‘s still working. This is very different from Java Script, where breaking the statement over several lines changes its meaning completely.
PHP relies on the closing semi colon to know when it‘s reached the end of a command. So, if I remove the comment marks from these last two Echo commands, and save the page, and we reload, you can see that the output is exactly the same as it was before. Now, obviously, putting this amount of Whitespace in code is ridiculous, but it does mean that you can use Whitespace within your code to lay it out for greater readability. So there you have it, comments on the use of Whitespace in PHP.
Comments and whitespace