Which of the following can be used as comments in php?


Comments in PHP

A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be read by someone who is looking at the code.

Comments can be used to:

  • Let others understand your code
  • Remind yourself of what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code

PHP supports several ways of commenting:

Example

Syntax for single-line comments:



// This is a single-line comment

# This is also a single-line comment
?>


Try it Yourself »

Example

Syntax for multiple-line comments:



/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>


Try it Yourself »

Example

Using comments to leave out parts of the code:



// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>


Try it Yourself »



J. Prettyman

8 years ago

Notes can come in all sorts of shapes and sizes. They vary, and their uses are completely up to the person writing the code. However, I try to keep things consistent in my code that way it's easy for the next person to read. So something like this might help...

//======================================================================
// CATEGORY LARGE FONT
//======================================================================

//-----------------------------------------------------
// Sub-Category Smaller Font
//-----------------------------------------------------

/* Title Here Notice the First Letters are Capitalized */

# Option 1
# Option 2
# Option 3

/*
* This is a detailed explanation
* of something that should require
* several paragraphs of information.
*/

// This is a single line quote.

?>

M Spreij

17 years ago

A nice way to toggle the commenting of blocks of code can be done by mixing the two comment styles:
//*
if ($foo) {
  echo
$bar;
}
// */
sort($morecode);
?>

Now by taking out one / on the first line..

/*
if ($foo) {
  echo $bar;
}
// */
sort($morecode);
?>
..the block is suddenly commented out.
This works because a /* .. */ overrides //. You can even "flip" two blocks, like this:
//*
if ($foo) {
  echo
$bar;
}
/*/
if ($bar) {
  echo $foo;
}
// */
?>
vs
/*
if ($foo) {
  echo $bar;
}
/*/
if ($bar) {
  echo
$foo;
}
// */
?>

magnesium dot oxide dot play+php at gmail dot com

8 years ago

It is worth mentioning that, HTML comments have no meaning in PHP parser. So,

WILL execute some_function() and echo result inside HTML comment.

hcderaad at wanadoo dot nl

17 years ago

Comments in PHP can be used for several purposes, a very interesting one being that you can generate API documentation directly from them by using PHPDocumentor (http://www.phpdoc.org/).

Therefor one has to use a JavaDoc-like comment syntax (conforms to the DocBook DTD), example:
/**
* The second * here opens the DocBook commentblock, which could later on

* in your development cycle save you a lot of time by preventing you having to rewrite

* major documentation parts to generate some usable form of documentation.
*/
?>
Some basic html-like formatting is supported with this (ie
tags) to create something of a layout.

J Lee

16 years ago

MSpreij (8-May-2005) says  /* .. */ overrides // 
Anonymous (26-Jan-2006) says // overrides /* .. */

Actually, both are correct. Once a comment is opened, *everything* is ignored until the end of the comment (or the end of the php block) is reached.

Thus, if a comment is opened with:
   //  then /* and */ are "overridden" until after end-of-line
   /*  then // is "overridden" until after */

Steve

17 years ago

Be careful when commenting out regular expressions.

E.g. the following causes a parser error.

I do prefer using # as regexp delimiter anyway so it won't hurt me ;-)

/*

$f->setPattern('/^\d.*/

');

*/

?>

theblazingangel at aol dot com

15 years ago

it's perhaps not obvious to some, but the following code will cause a parse error! the ?> in //?> is not treated as commented text, this is a result of having to handle code on one line such as echo 'something'; //comment ?>

if(1==1)
{
   
//?>
}
?>

i discovered this "anomally" when i commented out a line of code containing a regex which itself contained ?>, with the // style comment.
e.g. //preg_match('/^(?>c|b)at$/', 'cat', $matches);
will cause an error while commented! using /**/ style comments provides a solution. i don't know about # style comments, i don't ever personally use them.

jballard at natoga dot com

11 years ago

Comments do NOT take up processing power.

So, for all the people who argue that comments are undesired because they take up processing power now have no reason to comment ;)

// Control
echo microtime(), "
"
; // 0.25163600 1292450508
echo microtime(), "
"
; // 0.25186000 1292450508

// Test

echo microtime(), "
"
; // 0.25189700 1292450508
# TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST
# .. Above comment repeated 18809 times ..
echo microtime(), "
"
; // 0.25192100 1292450508?>

They take up about the same amount of time (about meaning on a repeated testing, sometimes the difference between the control and the test was negative and sometimes positive).

Wolfsbay at ya dot ru

12 years ago

If you are using editor with code highlight, it’s much easier to notice error like /* */ */.

fun at nybbles dot com

16 years ago

a trick I have used in all languages to temporarily block out large sections (usually for test/debug/new-feature purposes), is to set (or define) a var at the top, and use that to conditionally comment the blocks; an added benefit over if(0) (samuli's comment from nov'05) is that u can have several versions or tests running at once, and u dont require cleanup later if u want to keep the blocks in:  just reset the var.

personally, I use this more to conditionally include code for new feature testing, than to block it out,,,, but hey, to each their own :)

this is also the only safe way I know of to easily nest comments in any language, and great for multi-file use, if the conditional variables are placed in an include :)

for example, placed at top of file:

= TRUE
      
$debug2 = FALSE;
?>

and then deeper inside the file:

if ($ver3) {
           print(
"This code is included since we are testing version 3");
         }
?>

if ($debug2) {
           print(
"This code is 'commented' out");
         }
?>

anisgazig at gmail dot com

2 years ago

In php there are 3 types of comments
1.single line c++ style comment(//)
2.single line Unix shell stype comment(#)
3.multi line c style comment(/*/)

single or multi line comment comes to the end of the line or come first to the current block of php code.

HTML code will be printed after //...?> or #...?>
closing tag breaks the php mode and return to html mode.

different comments in different tags:
===================================

Standard tag: //echo " standard tag"; ?>single line c++ style comment


The header above will break php mode and return html mode and show  'Standard tag:single line c++ style comment'

Standard tag: # echo " standard tag"; ?>single line unix shell style comment


The header above will break php mode and return html mode and show  'Standard tag:single line unix shell style comment'

Standard tag: /*echo " standard tag"; */?>multi line c style comment


The header above will break php mode and return html mode and show  'Standard tag:multi line c style comment'

 

short echo tag: // " short echo tag"; ?>single line c++ style comment


The header above will break php mode show a unexpected syntex error'

 

short echo tag: #  " short echo tag"; ?>single line c++ style comment


The header above will break php mode show a unexpected syntex error'

 

short echo tag: /*echo " short echo tag"*/ ; ?>multiple  line c style comment


The header above will break php mode show a unexpected syntex error'

Short tag: single line c++ style comment


The header above will break php mode and return html mode and show  'Short tag:single line c++ style comment'

 

Short tag: single line unix shell style comment


The header above will break php mode and return html mode and show  'Short tag:single line unix shell style comment'

  

Short tag: multi line c style comment


The header above will break php mode and return html mode and show  'Short tag:multi line c style comment'

   

Script tag: single line c++ style comment


The header above will break php mode and return html mode and show  'Script tag:single line c++ style comment'

   

Script tag: multi line c style comment


The header above will break php mode and return html mode and show  'Script tag:multi line c style comment'

   

Script tag: single line unix shell style comment


The header above will not break php mode

What is used for comments in PHP?

Single-line PHP Comments To leave a single-line comment, type two forward slashes (//) followed by your comment text. All text to the right of the // will be ignored. You can also use a hash symbol (#) instead of // to make a single-line comment.

Which of the following is correct to add a comment in PHP?

Which of the following is the correct way to add a comment in PHP code? Explanation: In PHP, /* */ can also be used to comment just a single line although it is used for paragraphs. // and # are used only for single-line comments.

Which of the following is a single line comment of PHP?

PHP Comment Syntax: Single Line Comment To do a single line comment type "//" or "#" and all text to the right will be ignored by PHP interpreter.

Can you comment through multiple lines in PHP?

In PHP, we can comments multiple lines also. To do so, we need to enclose all lines within /* */. Let's see a simple example of PHP multiple line comment.