PHP 5.4.31 Released

Commenti

PHP supporta i commenti dei linguaggi 'C', 'C++' e stile shell (stile Perl) di Unix. Per esempio:

<?php
    
echo 'Questo è un test'// Questo è un commento su una linea nella stile c++ 
    /* Questo è un commento su più linee
       ancora un'altra linea di commento */
    
echo 'Questo è un altro test';
    echo 
'Un ultimo test'# Questo è un commento stile shell Unix 
?>

Lo stile di commento su "una linea", attualmente commenta solo fino alla fine della linea o del blocco corrente di codice PHP. Questo significa che l'eventuale codice HTML posto dopo // ... ?> o # ... ?> sarà stampato: ?> esce dalla modalità PHP e ritorna in modalità HTML, e quindi // non può influenzare quest'ultima. Se l'impostazione asp_tags è abilitata, il PHP si comporta allo stesso modo con // %> e # %>. Tuttavia il tag </script> non esce dalla modalità PHP nei commenti di una linea.

<h1>Questo è un <?# echo 'semplice';?> esempio.</h1>
<p>L'intestazione qui sopra dirà 'Questo è un esempio'.</p>

I commenti in stile C terminano alla prima occorrenza di */. Occorre fare attenzione nel non annidare i commenti di stile C, situazione che si presenta quando si commentano larghi blocchi di codice.

<?php
 
/*
    echo 'Questo è un test'; /* Questo commento causerà dei problemi */
 
*/
?>

add a note add a note

User Contributed Notes 18 notes

up
88
M Spreij
9 years ago
A nice way to toggle the commenting of blocks of code can be done by mixing the two comment styles:
<?php
//*
if ($foo) {
  echo
$bar;
}
// */
sort($morecode);
?>

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

<?php
/*
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:
<?php
//*
if ($foo) {
  echo
$bar;
}
/*/
if ($bar) {
  echo $foo;
}
// */
?>
vs
<?php
/*
if ($foo) {
  echo $bar;
}
/*/
if ($bar) {
  echo
$foo;
}
// */
?>
up
26
J. Prettyman
5 months 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...

<?php

//======================================================================
// 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.
?>
up
28
hcderaad at wanadoo dot nl
9 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:
<?php
/**
* The second * here opens the DocBook commentblock, which could later on<br>
* in your development cycle save you a lot of time by preventing you having to rewrite<br>
* major documentation parts to generate some usable form of documentation.
*/
?>
Some basic html-like formatting is supported with this (ie <br> tags) to create something of a layout.
up
23
magnesium dot oxide dot play+php at gmail dot com
9 months ago
It is worth mentioning that, HTML comments have no meaning in PHP parser. So,

<!-- comment
<?php echo some_function(); ?>
-->

WILL execute some_function() and echo result inside HTML comment.
up
12
Steve
9 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 ;-)

<?php

/*

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

*/

?>
up
6
J Lee
8 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 */
up
11
fun at nybbles dot com
8 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:

<?php $ver3 = TRUE
      
$debug2 = FALSE;
?>

and then deeper inside the file:

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

<?php if ($debug2) {
           print(
"This code is 'commented' out");
         }
?>
up
6
theblazingangel at aol dot com
6 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 <?php echo 'something'; //comment ?>

<?php
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.
up
3
Wolfsbay at ya dot ru
4 years ago
If you are using editor with code highlight, it’s much easier to notice error like /* */ */.
up
3
Anonymous
8 years ago
M Spreij wrote, 08-May-2005 08:15...
A nice way to toggle the commenting of blocks of code can be done by mixing the two comment styles:
...
This works because a /* .. */ overrides //.

The final sentence should be the other way round, i.e.

This works because a // overrides /* .. */.
(If it didn't the /* .. */ would comment out the code regardless of whether an additional '/' is prefixed to the first line).
up
1
jballard at natoga dot com
3 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 ;)

<?php

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

// Test
echo microtime(), "<br />"; // 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(), "<br />"; // 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).
up
0
Anonymous
9 months ago
The comment priority is depended on the order:
1. */
2. ?>
3. // and #
up
0
mst_NO_SPAM_TO_ME at mstsoft dot com
8 years ago
This "comment ends on line break or end of PHP Block" thing can be confusing. I discovered this by accident when working with XML Output from PHP...

<?PHP

header
("Content-type: text/xml");

/*
echo "<?xml version=\"1.0\"?>";
echo "<page>multi-line comments work as expected.</page>";
*/

//echo "<?xml version=\"1.0\"
?>";
//echo "<page>single-line comments end php mode and output your code.</page>";

?>

I would expect the comment to work, but there is no parsing in comments so the String suddenly becomes a PHP  end-block tag, which is correct reading this documentation.

cheers,
martin
PS: You even see the behavior in the Syntax highlighting :-)
up
-5
Clem at no dot spam
1 year ago
Uncommented:

<?php
/**/

echo "foo";

/**/
?>

Commented:

<?php
/** /

echo "foo";

/**/
?>
up
-6
team at researchbib dot com
2 years ago
when the comment string contains '?>', you should be careful.

e.g. output code 1= code 2 is different with code 3
1. with //
<?php

// echo '<?php ?>';

?>

2. with #

<?php

// echo '<?php ?>';

?>

3. with /* */

<?php

/* echo '<?php ?>';*/

?>
up
-8
philip-php at dago dot yourweb dot de
3 years ago
It's true, comments do not take up PROCESSING time, but they do take some PARSING time in case you are not using a compile cache of some kind.
up
-15
samuli dot karevaara at lamk dot fi
8 years ago
If you want to comment out large sections of code (temporarily, usually and hopefully), consider using
<?php
if (0) {
     print(
"This code is 'commented' out");
}
?>
instead of /* comment block */. Otherwise, as noted here, you will have parse errors if the block that you commented out contains */ somewhere, like in regexp or in another comment.
up
-17
benny at bennyborn dot de
3 years ago
This regex should do the job when trying to parse comments

(\/\*(.*?)\*\/)|(^|\s+)\/\/(.*?)(\n|$)|(^|\s+)#(.*?)(\n|$)
To Top