Comments and whitespace

Source: Internet
Author: User
Tags echo command

In this video, I'll show you the varous ways of adding comments to PHP scripts.

Below are several comments:

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.

Obviusly, 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. (For some code, if you are sure you cannot use it, delete it. Otherwise, you must enter it again.) 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 cocould 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 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 cocould do is, I cocould 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 several 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.