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.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *

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