How to Comment in PHP

Use single-line comments for short comments., Use multi-line comments for longer comments or code testing., Use comments to leave notes about how code works., Leave comments so that you remember what you were doing., Comment on code you intend to...

8 Steps 1 min read Medium

Step-by-Step Guide

  1. Step 1: 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.<?php // This is the standard (C++) way to create a single-line comment # You can also use this Unix style to create a single-line comment ?>
  2. Step 2: 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.<?php /* This is how you format a multi-line comment.

    Everything until the ending tag will be included in the comment */ /* Some people like to include * extra markers at the beginning * of each line.

    This can help with readability * for large comments, but isn't necessary. */ ?>

    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 POST curl_setopt ($session, CURLOPT_POST, true)
  3. Step 3: Use comments to leave notes about how code works.

    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 on echo "Hello World!"
  4. Step 4: Leave comments so that you remember what you were doing.

    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" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male , 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. <?php echo "/*Hello*/ World!"; /* The word "Hello" will not be displayed when the above code is run */ ?>

    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. <?php /* echo "Hello World!"; /* This comment will mess things up */ */ ?> <?php /* echo "Hello World!"; // This comment will be fine */ ?>

    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. <?php //============= // HEADING //============= //------------- // Sub-Heading //------------- /* Title Of Section */ # Documentation can go here # A second piece can go here /* * Use these for explaining something * that would take several lines or * even multiple paragraphs to explain */ ?>
  5. Step 5: Comment on code you intend to share.

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

  7. Step 7: Be careful when commenting out large blocks of code.

  8. Step 8: Use comments to create pseudo-documentation.

Detailed Guide

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.<?php // This is the standard (C++) way to create a single-line comment # You can also use this Unix style to create a single-line comment ?>

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.<?php /* This is how you format a multi-line comment.

Everything until the ending tag will be included in the comment */ /* Some people like to include * extra markers at the beginning * of each line.

This can help with readability * for large comments, but isn't necessary. */ ?>

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 POST curl_setopt ($session, CURLOPT_POST, true)

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 on echo "Hello World!"

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" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male , 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. <?php echo "/*Hello*/ World!"; /* The word "Hello" will not be displayed when the above code is run */ ?>

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. <?php /* echo "Hello World!"; /* This comment will mess things up */ */ ?> <?php /* echo "Hello World!"; // This comment will be fine */ ?>

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. <?php //============= // HEADING //============= //------------- // Sub-Heading //------------- /* Title Of Section */ # Documentation can go here # A second piece can go here /* * Use these for explaining something * that would take several lines or * even multiple paragraphs to explain */ ?>

About the Author

J

Jack King

Jack King specializes in technology and innovation and has been creating helpful content for over 4 years. Jack is committed to helping readers learn new skills and improve their lives.

96 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: