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

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

How to Comment Code in HTML, CSS, JavaScript, PHP, and MySQL

When designing or debugging a website, commenting your code is a valuable habit. Comments can help:

  • Explain complex code

  • Temporarily disable code without deleting it

  • Leave notes for collaborators (or your future self!)

  • Diagnose issues

This guide explains how to use comments across various web technologies, including HTML, CSS, JavaScript, PHP, and MySQL.

HTML Comments

HTML comments are used to add notes or hide code inside .html files. These are not visible to users in the browser.

Syntax:

<!-- This is a comment --> <!-- <h1>This heading is hidden</h1> -->

Example:

<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 Comments

CSS comments are used in external .css files or inside <style> tags within an HTML file.

✅ Syntax:

/* This is a CSS comment */

✅ Example:

/* Hide the element */ .hidden { display: none; }

JavaScript Comments

JavaScript supports two types of comments: single-line and multi-line.

Syntax:

// Single-line comment /* Multi-line comment */

Example:

// This function adds two numbers function add(a, b) { return a + b; }

PHP Comments

PHP supports the same types of comments as JavaScript.

Syntax:

// Single-line comment # Another single-line comment (less common) /* Multi-line comment */

Example:

<html><body> <?php // This is a comment echo '<p>HTML code echoed by PHP.</p>'; /* This part is commented out: echo '<h1>Title</h1>'; echo '<p>This will not run.</p>'; */ ?> </body></html>

MySQL Comments

MySQL supports three types of comments and executable comments:

1. Double Dash (--)

Note: Must be followed by a space or control character.

SELECT * FROM users; -- This is a comment

2. Hash (#)

SELECT * FROM employees; # Get all employees

3. C-style (/* */)

Used for multi-line comments.

/* Get sales reps reporting to Anthony */ SELECT lastName, firstName FROM employees WHERE reportsTo = 1143 AND jobTitle = 'Sales Rep';

⚠️ MySQL does not support nested comments.

Executable Comments in MySQL

MySQL supports executable comments that only execute in MySQL (ignored by other database systems). This is useful for cross-database compatibility.

Syntax:

/*! MySQL-specific code */

Example:

SELECT 1 /*! +1 */; -- Returns 2 in MySQL, 1 in other systems

With Version-Specific Execution:

/*!50110 KEY_BLOCK_SIZE=1024; */ -- Only executes in MySQL version 5.1.10 or later

Summary Table

LanguageSingle-line CommentMulti-line Comment
HTML<!-- comment -->Not supported
CSSNot supported/* comment */
JavaScript// comment/* comment */
PHP// or #/* comment */
MySQL-- comment, # comment/* comment */, /*! comment */
Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close