How to Comment in PHP

A comment is a type of annotation that can be used to clarify the purpose and intent of a piece of code. When using PHP, you have several options to choose from that stem from popular older languages with two choices of single line comments and also a multi-line C-style comment. You can use comments to keep sections of code from running, and can even use them to create documentation.

Styles

Use single-line comments for short comments.

If you need to leave a short comment, you can use the single-line comment code. The comment will only last to the end of the line or the end of the code block. These comments only work within PHP tags, and will be read if placed in HTML.

Use multi-line comments for longer comments or code testing.

Multi-line comments are useful for writing a long explanation, or for preventing a segment of code from being processed. See the “Usage” section below for some tips on using multi-line comments.

Usage

Use comments to leave notes about how code works.

You shouldn’t have to do this for every line of code, since good code should be fairly easy to parse by other programmers. It is useful if the code is performing irregular or not obvious functions.// Generate curl request$session = curl_init($request);// Tell curl to use HTTP POSTcurl_setopt ($session, CURLOPT_POST, true);

Leave comments so that you remember what you were doing.

Whenever you’re working on your own projects, comments can help you remember where you left off. Leave comments on code that isn’t working properly, or that you haven’t finished yet. // Need to revisit the output for this before moving onecho “Hello World!”;

Comment on code you intend to share.

If you plan on collaborating with others, or intend to make your code open-source, comments can help others figure out how your code functions and the best places to make improvements./* Is there a more effective way to accomplish this? */Gender:<input type=”radio” name=”gender”value=”female”>Female<input type=”radio” name=”gender”value=”male”>Male

Use comments to keep specific blocks of code from being run.

This is useful if you’re testing something and need to prevent certain code from being run. Anything contained within the comment tags will be ignored when the page is loaded.

Be careful when commenting out large blocks of code.

The comment function will end whenever the first ending tag is hit, so if there’s already a multi-line comment inside the code you comment out, the comment will only last until the end of the original nested comment.

Use comments to create pseudo-documentation.

You can use some creative code formatting to create documentation for your code directly in the code. This can be useful for open-source projects.

Tips

  • HTML comments and PHP comments are both different, so when you are using scripting (HTML and PHP mix), take care that you aren’t mixing comments.
  • For example, in the below code, there is an HTML comment, but it still executes PHP code. Placing an HTML comment within PHP tags will cause errors.
    <!–<div id=”example”><?php echo ‘hello’; ?></div>–>

Leave a Comment