Comment in code: R, SQL, PHP, CSS, HTML, Stata

Image of blog post and discussion

Purpose: to comment in code in various software and coding languages

This page gives a quick reference guide and additional details on how to comment and uncomment code in various software and programming languages, including R, SQL, PHP, CSS, HTML and Stata.

What are comments in code?

Comments in code refer to text in the code which is not run or activated by the software or language.

Comments can be used to annotate the code for the programmer and others wanting to use the code, and to keep versions of code chunks without having to delete them.

Commenting in code is essential!

Commenting in code is essential for a number of reasons:

  • Notate code for your own and others’ reference (avoids coming back to previously written code, or using someone else’s code, and thinking “Er, what does that do??“);
  • Keep test versions or iterations of code for future use – it’s much better to keep a copy of a chunk of code, rather than edit and overwrite it, and then find that your edits have broken the code and you can’t get back to your earlier version (grr!!);
  • Comment out sections of code while testing other parts;
  • To log the status and progress of the code – keeping track of author, edits, versions, to-do list, etc.
  • ‘Hide’ code from the software or compiler. Code can be hidden from some software but will be activated when run in another software (e.g. javascript inside HTML code).

Good reasons to use a code editor

Code editors are so useful when creating and editing code. A code editor can act as a kind of scratchpad while you’re creating lines and chunks of code.

If you’re starting out in coding or using formulas in Excel, see our Excel Beginner tips page here.

Features of code editors:

  • they highlight syntax (different color texts for different elements of the code)
  • use line numbering,
  • can collapse chunks of code to help with navigation through long scripts,
  • indent text to make it easy to see code structure,
  • show related open and close brackets,
  • provide advanced search and replace features,
  • open files from different programs at the same time (useful for cross-platform working),
  • generally help with creating and finding errors in code.

Notepad++ code editor

Notepad++ is one of the most popular open source code editors for Windows.

It comes with syntax highlighting for many languages including R, SQL, PHP, Javascript, CSS and HTML.

Notepad++ — keep ‘unsaved’ files

One of the features I have found most helpful is that you can open new files and Notepad++ will automatically save them for later (with default filenames, Untitled1, etc.).

You can close Notepad++ without being prompted to save all the newly created files, and then when you re-open the program all your files are still there.

Sounds simple, but if you’ve ever started lots of back-of-the-envelope script ideas and wanted to quickly exit a program, it can be really annoying to have to save every single file!

Notepad++ comment and uncomment keyboard shortcuts

ActionShortcutHow to applyToggles (action when repeated)
Toggle single line commentCtrl+QClick anywhere in a line of codeYes
(repeated action switches on, then off, etc.)
Single line commentCtrl+KClick anywhere in a line of codeNo
(repeated action adds another comment wrapper around the line)
Single line UNcommentCtrl+Shift+KClick anywhere in a line of commented code No
Will remove all layers of commenting (e.g. if you inadvertently add more than one comment wrapper, all comment wrappers are removed).
Block commentCtrl+Shift+QSelect all lines to comment outNo
Block UNcommentCtrl+Shift+KClick in any line within the commented blockNo
Comment tags wrapping the whole block will be removed.

Create comments in code (R, SQL, CSS etc.) — quick reference table

Below is a quick reference guide to the various symbols and characters used to denote comments in code.

Coming soon … see more details below regarding keyboard shortcuts, single and multiple (block) commenting, and how to comment and uncomment.

Software / languageSymbols / characters used for single and/or multiple linesNotes
CSS/* comment */
(single and multiple)
[forward slash, asterisk] to open comment,
[asterisk, forward slash] to close comment
Excel formulas'[single apostrophe] before comment
Excel VBA and macros'[single apostrophe] before comment
HTML<!-- comment -->
(single and multiple)
[less than, exclamation mark, two dashes] to open the comment,
[two dashes, greater than] to close the comment
Javascript//comment (single)
/* comment */ (multiple)
[two forward slashes] before single line comment
[forward slash, asterisk] to open comment, [asterisk, forward slash] to close comment for wrapping a comment block
#comment (single)
/* comment */ (multiple)
[two forward slashes] or [hash] for a single line;
[forward slash, asterisk] to open comment, [asterisk, forward slash] to close comment for wrapping a comment block
Python#[hash] per line of comment
R programming#[hash] per line of comment
SQL-- (single)
/* */ (multiple)
[double dash] per line for single;
[forward slash, asterisk] to open comment, [asterisk, forward slash] to close comment
Stata*[asterisk] per line of comment

Coming soon! More details on how to comment and uncomment when coding!