How and Why to Comment in Your PHP Code

Posted by

How and Why to Comment in Your PHP Code

A comment in PHP code is a line that is not read to be as part of the programs. Its only purpose to be read by someone who is now editing the code. So why you use comments?

To let others, know on what you are doing. If you are working with group of people or plan on anyone else using your script, then the comments tell that other programmers on what you were doing at each step. This makes much easier for them to work with and to edit your code if needed.

In order to remind yourself what has done. You may just be writing a quick script and don’t see that the need for comments might go ahead and add it in anyway. Most programmers then must have experienced coming to edit their own work a year or two later to figure out what they have did. Comments must remind you about your thoughts when you write the code.

There are several ways in adding a comment in PHP code. First is by using // to comment out a line. This is one-line comment style only comments at the end of line or current code block whichever comes first. Here is the best example:

   <? php

 echo “hello”;

 //this is a comment

 echo ” there”;

 ?>

If you have a single line comment, then another option to use a #sign. Here is the best example for this method:

 <? php

 echo “hello”;

 #this is a comment

 echo ” there”;

 ?>

If you have a longer, multi-line comment, best way to comment is with /* and */ before and after a lengthy comment.

You can contain several lines on commenting inside a block. Here is an example:

 <?php

 echo “hello”;

 /*

 Using this method

 you can create a larger block of text

 and it will all be commented out

 */

 echo ” there”;

 ?>

Don’t Mix Comments

You can now nest comments with comments in PHP do so carefully. Not all of them nest equally well. PHP supports C, C++ and Unix shell-style comments. C style comments end at first */ they encounter, so don’t nest C style comments.

If you are working up with HTML and PHP which are aware that HTML comments which mean nothing to PHP parser. They won’t work as intended to execute some function.

<! –Comment–>

Using PHP and HTML on the Same Page

There are two ways to use HTML on your PHP page. First way to put HTML outside of your PHP tags.  You can even put it into the middle if you close and reopen up the <?php and ?> tags. There is an example in putting the HTML outside of the tags:

<html>

 <title>HTML with PHP</title>

 <body>

 <h1>My Example</h1>

 

 <?php

 //your php code here

 ?>

 <b>Here is some more HTML</b> 

 <?php

 //more php code

 ?> 

 </body>

 </html>

As you can able to see you any HTML you want without doing any extra or special in your .php file as long as its outside of PHP tags.

Second way to use HTML with PHP using PRINT or ECHO. Using this method, you can able to include HTML inside the PHP tags. There is a quick and best method that if you only have a line to do so. Here is an example:

 <?php

 Echo “<html>”;

 Echo “<title>HTML with PHP</title>”;

 Echo “<b>My Example</b>”;

  //your php code here

  Print “<i>Print works too!</i>”;

 ?>

 Using one or both these methods then you can easily embed HTML code on to your PHP pages to give nicer more formatted look to make them user friendly one.

Leave a Reply

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