COMMENTS

COMMENTS
A few words about the comments.

Comments are used, as the name suggests, for commenting sections of code. With their help you can efficiently document the created code and enable quick preview of documentation for other developers.

Wait.. What?

What does it mean to document the code?

Let’s say that documentation of the code is just providing some tips for other developers of how your application is written. No worries, we’ll get to that later.

The most important thing for you at this point is that the added comment is ignored by the PHP interpreter. It means you can leave a note that will not be displayed on the page and no one will be able to see it in the browser.

WHY YOU USE COMMENTS?

The principle is simple – let’s comment anything that can be difficult to understand after some time.

Generally, well-written code does not require a lot of commenting. Especially, you shouldn’t comment such things as variable’s name or why you multiply some values. The names of variables, functions and files should be as descriptive as they can. You will get used to it, eventually.

There is a term for such clear and understandable code:

self-documenting code
Code that is written so well that no additional comments are needed to understand it properly.
It means that the names of functions, classes and variables are so clear and accurate that they logically explain the behavior of the program, step by step.

We will have another article about writing understandable code. First of all, you have to start writing any code.

HOW TO PLACE COMMENTS IN THE PHP CODE?

There are two ways to place comments in PHP code.

The first one is used when we want to comment more than one line of text. We then place such a text block between the characters /* and */. Anything between these tags will be ignored by the interpreter when generating html code.

<?php
/* this is a comment
that covers multiple lines
and won't be sent to
client's browser */
Once again, I would like to stress here that comments are not being sent to user’s browser.

The second way to comment is to put text after two forward slashes //.

<?php
// this is one-line comment that comments everything after doubleslashes
// but ends with end of line
// so you have to repeat it with every line you want to comment
It is a faster and more convenient way than the first one, because you don’t need to place any characters that close the comment. Everything that is in the same line, after double slashes, is treated as a comment. However, this type of commenting is suitable only for one line, so if you have a long text consisting of several lines, use the first method.

EXCLUDING PART OF THE CODE

In the modern code editor you will find a keyboard shortcut that automatically marks whole selection as a comment. With the other key combination you can get rid of the comments.

In this way you have learned about the most popular application of comments, i.e. excluding part of the code so that it does not execute.

<?php
// I don't want to execute next echo function
// so I can comment it like that:
// echo "Hi, this is a text that won't be displayed.";
echo "This echo will work as expected.";
Sometimes script you write doesn’t work as expected. Then you have to identify where the problem is. The easiest way is just to comment most part of the code. Then uncomment line after line and check what is causing the issue.

So comments are useful not only for documenting and describing functionalities.

It is very valueable development tool that is used mostly during the writing of the code and debugging.

Without it you would have to delete parts of code or try to copy/paste them to another file. It’s only worth to mention at the end that you don’t want to keep commented code in the final version of the application.

SHARE THIS:

Comments

Popular posts from this blog

FIRST LINE OF CODE IN PHP

VARIABLES AND CONSTANTS

SIMPLE PHP