How do I comment my HTML, CSS, JavaScript, and PHP code?

 Comment on my HTML, CSS, JavaScript, and PHP code?

When you are designing your website, there are many instances that require you to comment out code in your website. Commenting code is good for diagnosing website problems, hiding code instead of deleting the code, placing notes in a file to explain what was done, and many more reasons. This article will explain the basics of commenting code in your website files.

HTML Comments

HTML comments are specifically designed to comment out code when you are in an HTML-coded document. Below is the syntax for HTML comments.

HTML comment

<!-- <code to be commented out>  -->

Example of an HTML comment

An Example of a usage of this is shown in the following code.

<html><head>
<title>Document</title> 
</head><body>
<!-- This is a comment -->
<p>A paragraph in your site</p>
<!-- This is code commented out <h1>Commented Title</h1> <p>A paragraph that is commented out.</p> -->
</body></html>

CSS, JavaScript, and PHP Comments

CSS comments are done in the CSS .css stylesheet or in the Internal Style sheet. JavaScript comments can be placed inside HTML documents or in a .js file where JavaScript is run. PHP code will be in .php files between <?php and ?> code. The following code shows the syntax for commenting out CSS, JavaScript, and PHP code.

Single Line comment

// <code to be commented out>

Multiple Line comment

/* <code to be commented out> <Other code to be commented out> <Some more code to be commented out> */

Example of PHP comment

An Example of a usage of this is shown in the following code. Below is showing PHP code; however, you use the same process to comment on JavaScript and CSS.

<html><body> 
<?php   
// This is a comment on one line 
echo '<p>HTML code echoed by PHP.</p>'; /* This is code commented out echo '<h1>An HTML Title from PHP</h1>'; echo '<p>HTML code echoed by PHP.</p>'; */ ?> 
</body></html>

MySQL supports three comment styles:

  1. From a '-- ' to the end of the line. The double dash-comment style requires at least whitespace or control character (space, tab, newline, etc) after the second dash.
    SELECT * FROM users; -- This is a comment
    

    Note that standard SQL does not require whitespace after the second dash. MySQL uses whitespace to avoid the problems with some SQL construct such as:

    SELECT 10--1;
    

    The statement returns 11. If MySQL didn’t use the whitespace, it would return 10 instead.

  2. From a '#' to the end of the line.
    SELECT 
        lastName, firstName
    FROM
        employees
    WHERE
        reportsTo = 1002; # get subordinates of Diane
    
  3. C-style comment /**/ can span multiple lines. You use this comment style to document a block of SQL code.
    /*
        Get sales rep employees
        that reports to Anthony
    */
    
    SELECT 
        lastName, firstName
    FROM
        employees
    WHERE
        reportsTo = 1143
            AND jobTitle = 'Sales Rep';
    

Notice that MySQL does not support nested comments.

Executable comments

MySQL provides executable comments to support portability between different databases. These comments allow you to embed SQL code that will execute only in MySQL but not other databases.

The following illustrates the executable comment syntax:

/*! MySQL-specific code */
Code language: SQL (Structured Query Language) (sql)

For example, the following statement uses an executable comment:

SELECT 1 /*! +1 */
Code language: SQL (Structured Query Language) (sql)

The statement returns 2 instead of 1. However, it will return 1 if you execute it in other database systems.

If you want to execute a comment from a specific version of MySQL, you use the following syntax:

/*!##### MySQL-specific code */
Code language: SQL (Structured Query Language) (sql)

The string ‘#####’ represents the minimum version of MySQL that can execute the comment. The first # is the major version e.g., 5 or 8. The second 2 numbers (##) are the minor version. And the last 2 is the patch level.

For example, the following comment is only executable in MySQL 5.1.10 or later:

CREATE TABLE t1 ( k INT AUTO_INCREMENT, KEY (k) ) /*!50110 KEY_BLOCK_SIZE=1024; */
Code language: SQL (Structured Query Language) (sql)

In this tutorial, you have learned how to use MySQL comments to document the SQL code in MySQL.

Post a Comment

Previous Post Next Post