Home » Tutorials » PHP » Comments

Comments

We arrive at the cornerstone of all languages, including PHP, comments. I cannot stress the importance of commenting your code. As a webmaster, I create all kinds of web applications, but more importantly, I maintain the code previously crafted before I arrived. So many times, I have wondered “what the heck were they doing here?”…And after about 10-20 minutes of digesting their code, do I truly understand what they were trying to accomplish. Please comment. Please. With that being said, try to not over comment either. It is fine to do it on every few lines starting out as a beginner. However, when you get more advanced, try to comment large sections (each function and at least every 30 lines) and explain what you are doing in the following code.

Enough of the lecturing, how do I actually comment? Commenting is super easy just like in other languages. In PHP, you can make comments in two ways.

Comment Out One Line

Example <html>
<body>
<?php
//echo “I am awesome!”;
#echo “I am awesome!”;
echo “I am really awesome!”;
?>
</body>
</html>
Result I am really awesome!

Obviously, the // only comments until semicolon or until the end of the line. It does not require a closing partner. The # works exactly the same way as the previous comment. The other type of comment, is a block comment, which allows you to comment out more than one stupid line. It lets you comment out however much you want.

The Multiline Comment

Example <html>
<body>
<?php
/*echo “I am awesome!”;
echo “I am really awesome!”;
*/
echo “I am super awesome!”;
?>
</body>
</html>
Result I am super awesome!

The /* requires the closing tag of */. Everything in between these tags is completely ignored as PHP code. This comment is perfect for testing your code, blocking out code that is not needed right now, and writing a paragraph on what your following code is supposed to do.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Comments". After Hours Programming. Accessed on March 19, 2024. https://www.afterhoursprogramming.com/tutorial/php/comments/.

  • Stewart, Suzy. "Comments". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/php/comments/. Accessed 19 March, 2024.

  • Stewart, Suzy. Comments. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/php/comments/.



Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.