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:
Example:
CSS Comments
CSS comments are used in external .css
files or inside <style>
tags within an HTML file.
✅ Syntax:
✅ Example:
JavaScript Comments
JavaScript supports two types of comments: single-line and multi-line.
Syntax:
Example:
PHP Comments
PHP supports the same types of comments as JavaScript.
Syntax:
Example:
MySQL Comments
MySQL supports three types of comments and executable comments:
1. Double Dash (--
)
Note: Must be followed by a space or control character.
2. Hash (#
)
3. C-style (/* */
)
Used for multi-line comments.
⚠️ 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:
Example:
With Version-Specific Execution:
Summary Table
Language | Single-line Comment | Multi-line Comment |
---|---|---|
HTML | <!-- comment --> | Not supported |
CSS | Not supported | /* comment */ |
JavaScript | // comment | /* comment */ |
PHP | // or # | /* comment */ |
MySQL | -- comment , # comment | /* comment */ , /*! comment */ |