This page has been robot translated, sorry for typos if any. Original content here.

How to comment out the code in HTML, PHP, JavaScript, CSS, .htaccess?

Как закомментировать код

To comment out the code is to write comments in the code.

To comment out the code is to leave a text-cheat sheet for website developers to help them navigate the code faster. The comment in the code is invisible to the user.

So, what does it mean to comment out the code, and why this possibility was invented? To make things clear for you now, take a look at examples of how to comment out the HTML code, php, css, JavaScript, and .htaccess.

Attention: comments in the code for each programming language are prescribed differently. If you do not follow specific rules, your code may not work.

Comments in HTML code

<!-- Для HTML -->

HTML (HyperText Markup Language) is a standardized markup language for documents on the World Wide Web. Most web pages contain a description of markup in HTML (or XHTML). The HTML language is interpreted by browsers; The resulting formatted text is displayed on the screen of a computer monitor or mobile device.

In the World Wide Web, HTML pages are usually transmitted to browsers from the server via HTTP or HTTPS protocols, as plain text or using encryption.

Example with application:

  <html>
 <head>
 <title> Comments in HTML code </ title>
 </ head>
 <body>
 <! - Site header ->
 <img src = "kartinka.png">
 <! - the end of the site header ->
 </ body>
 </ html>

Comments in PHP code

/* 1. Многострочный комментарий для PHP */ // 2. Однострочный комментарий для PHP # 3. Однострочный комментарий для PHP

PHP (Hypertext Preprocessor - "PHP: Hypertext Preprocessor", originally Personal Home Page Tools - "Tools for creating personal web pages") is a general-purpose scripting language used extensively for developing Web applications. Currently supported by the vast majority of hosting providers and is one of the leaders among the languages ​​used to create dynamic Web sites.

The language and its interpreter (Zend Engine) are developed by a group of enthusiasts within the framework of an open source project. The project is distributed under its own license, which is incompatible with the GNU GPL.

Example with application:

  <? php
 / * 1. Multi-line comment for PHP * /
 echo "Example 1";  // 2. A single-line comment for PHP
 echo "Example 2";  # 3. A single-line comment for PHP
 ?>

Comments in the CSS code

/* комментарий в css */

CSS (English Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.

It is mainly used as a means of describing, visualizing the appearance of web pages written using markup languages ​​HTML and XHTML, but can also be applied to any XML documents, for example, to SVG or XUL.

Example with application:

  .p
 {
 font: 25px "Times New Roman", Times, serif;  / * Text font and size * /
 }

Comments in the JavaScript code

// Однострочный комментарий для JavaScript /* Многострочный комментарий для JavaScript */

JavaScript (abbreviated JS) is a multi-paradigm programming language. Supports object-oriented, imperative and functional styles. It is the implementation of the ECMAScript language (ECMA-262 standard).

JavaScript is commonly used as an embedded language for programmatic access to application objects. The widest application is found in browsers as a scripting language for giving interactivity to web pages.

Example with application:

  <script type = "text / javascript">
 / * Counting time * /
 var RemainsSec = (parseInt (totalRemains / 1000));  // how many seconds are left
 var RemainsFullDays = (parseInt (RemainsSec / (24 * 60 * 60)));  //days left
 / * End of countdown * /
 </ script>

Comments in the .htaccess code

# Описание инструкции

.htaccess is the Apache web server configuration file that allows you to manage the operation of the web server and site settings using various parameters (directives) without changing the main configuration file of the web server.

Directives for the .htaccess file are valid for the directory in which the file is located, and for all its subdirectories. Please, be careful when editing the .htaccess file! If you save such a file in UTF-8 encoding, there should be no BOM-signature in it.

Example with application:

  # Protection of comment from Spam bot
 RewriteEngine On
 RewriteCond% {REQUEST_METHOD} POST

Via bloggood.ru & wiki

Как закомментировать код

How to write code immediately with comments

In fact, the principle of TDD (Test-driven development, development through testing) has been moved to the level below. I do not remember how it sounded in the original, but in essence "Describe the structure of the code comments." On an example (strongly exaggerated, why - below) the code of the program, which adds two numbers, this principle will look like this:

  int main ()
 {
  // Accept two numbers from the user
  // Create a variable for the result of addition
  // Return the result of addition

  return 0;
 }

And only when the frame is prepared from the comments, it is necessary to write code that will implement what is described by the comments.

  ...
 int main ()
 {
  double a, b;
  // Accept two numbers from the user
  cin >> a;
  cin >> b;
  // Create a variable for the result of addition
  double sum = a + b;
  // Return the result of addition
  cout << sum;

  return 0;
 }

As mentioned above, this principle is a modified principle, a well-proven TDD. It should be understood that the deviation from the comments, in contrast to the deviation from the logic of the tests, will not lead to serious consequences, well, unless you have to rewrite the comments.

How to comment on an existing code

The answer to this question is quite simple: comment the entities from the parent to the descendant: class -> method -> code inside the method (if necessary) .

It is entirely logical to think: what not to comment on. You do not need to comment in two cases (one of them explains why the code example above was heavily exaggerated):

  • Absolutely obvious things. Comments of the form // initialize the counter more than their absence
  • The incomprehensible code is supplemented with a comment of type // nothing understood

Concerning the second item it is worth to explain a little and give an example: insert 100 lines of assembler code in C! You look at it and write a comment // Many bukaf! Nyasilil

After that, the person who came after your dismissal to your place sees this comment and ... everything! He will not even try to understand it, and this record will be stigmatized on this piece of code until it is removed (either code or comment).

At last

In conclusion, I can say that the art of writing comments is an integral part of the art of programming, that's why you need to write comments, and, how pathetic it may sound, but it is necessary to learn qualitative comments.