Tuesday, January 2, 2018

Used comment in PHP


Comments are make changes to PHP code that wrote long time ago. For the client might want something change or you might want to reuse a portion of code in a new project when returning to code. May not remember exactly why you did certain things the way  did for this reason. It is good practice to add comments to your code whenever possible. There are number of ways adding comments to your script but the two most common ways are to comment out a single line of code and comment out a entire block 
     Commenting out a single line is a simple matter of pre pending the line with two characters :- 

// This line will not be evaluated by PHP and is a comment 

To comment out an entire block, you could add the //  characters to the beginning of every line , but it is more convenient to use start and end characters for comment. The start and end character /* at the beginning of the comment and */ at the end.

          Comments give information to people who read  code, but they are ignored by PHP at execution time. Even if you think  the only person who will ever read code, it’s a good idea to include comments in  code—in retrospect, code wrote months ago could easily look as though a stranger wrote it. 
      A good practice is to make comments sparse enough not to get in the way of the code itself but plentiful enough that can use the comments to tell what’s happening. 
        Don’t comment obvious things, lest  bury the comments that describe tricky things. For example, this is worthless:
   $x = 17 ;       // store 17 into the variable $x 

whereas the comments on this complex regular expression will help whoever maintains your code:
// convert &#nnn; entities into 
characters $text = preg_replace ( '/&#([0-9])+;/e' , "chr(' \\ 1')" , $text ); 

PHP provides several types of comment using :-

  • C comments 
  • C++ comments
  • Shell style comments

C comment :- 

While shell-style and C++-style comments are useful for annotating code or making short notes, longer comments require a different style. As such, PHP supports block comments whose syntax comes from the C programming language. When PHP encounters a slash followed by an asterisk      ( /* ), everything after that, until it encounters an asterisk followed by a slash.    ( */ ), is considered a comment. This kind of comment, unlike those show nearlier, can span multiple lines.

For example :- 

/* In this section, we take a bunch of variables and 
       assign numbers to them. There is no real reason to do this, 
we're just having fun. 
$a = 1 ; 
$b = 2 ; 
$c = 3 ; 
$d = 4 ;

C++ comment :- 

When PHP encounters two slashes ( // ) within the code, everything from the slashes to the end of the line or the end of the section of code, whichever comes first, is considered a comment. This method of commenting is derived from C++. 
   The result is the same as the shell comment style. Here are the shell-style comment.

examples :-

// Cookie functions

if ( $doubleCheck ) {
// create an HTML form requesting that the user confirm the action 
echo confirmationForm ();
$value = $p * exp ( $r * $t ); // calculate compounded interest 

<? php $d = 4 ; // Set $d to 4. ?> Then another <?php echo $d; ?> Then another 4

Shell - style comment :- 

When PHP encounters a hash mark character ( # ) within the code, everything from the hash mark to the end of the line or the end of the section of PHP code (whichever comes first) is considered a comment. 
This method of commenting is found in Unix shell scripting languages and is useful for annotating single lines of code or making short notes. Because the hash mark is visible on the page, shell-style comments are sometimes used to mark off blocks of code:

 ## Cookie functions ####################### 

Sometimes they’re used before a line of code to identify what that code does, in which case they’re usually indented to the same level as the code: READ MORE


  1. This comment has been removed by a blog administrator.

    1. Dear ashish Thanks for comment have a nice day


Please comment and follow this site blog inbox