php.w3clubs.com

PHP tutorials and articles

 

PHP comments

You can have comments in your PHP code, which serve no other purpose, but help the programmer better understand what a specific part of the code is about. Comments are useful and important regardless if you're the only programmer working on a piece of code. A simple rule of thumb is - the more time a part of the code takes to write, the more it needs comments.

One-line comments

In any part of the script anything after // or # is ignored by PHP (except if it's in a string within quotes). For example:

// spitting out some date/time
echo 'Today is ', date('Y-m-d'); // printing the current date
echo ' and it is now ', date('H:i:s'); # the time

The result of this is

Today is 2007-09-06 and it is now 23:00:47

As you can see, everything after // and # was ignored.

In practice

In practice, # comments (Unix shell style comments) are very rarely used, most people use // (the C-style comments). So you're probably better off using only // for one-line comments.

Multi-line comments

You can put comments on multiple lines if you surround them in /* and */. An example:

  /*
  This code prints
  date and time
  */
  echo 'Today is ', date('Y-m-d'); // printing the current date
  echo ' and it is now ', date('H:i:s'); # the time

Documentation from comments

The tool called phpDoc can take comments written in a certain format and create documentation from them. This is pretty cool (you write documentation without actually writing it) and any decent size project should benefit from this tool.

Leaving PHP in a comment

If you have a PHP ending tag inside a single line comment, you'll leave PHP. Inside a multi-line comment the end tag is considered part of the comment and doesn't have the meaning of ending the PHP block. Example:

  <?php
  /*
  PHP code starts with <?php
  and end with ?>
  */

  // ?> ends a section of PHP code

  ?>

The result of this will be

ends a section of PHP code ?>

That's because the ?> in the single-line comment ended the script section, and everything after that is just normal plain HTML

« to the PHP tutorial table of contents

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress, theme by ♣w3clubs.com, based on Kubrik and Sancta Simplicitas