html
PHP suppors 'C', 'C++' and Unix shell-style (Perl style) commens. For example:
Example #1 Commens
<?php
echo
"This is a test\n"
;
// This is a one-line c++ style comment
/* This is a multi line comment
yet another line of comment */
echo
"This is yet another test\n"
;
echo
"One Final Test\n"
;
# This is a one-line shell-style comment
?>
The "one-line" comment styles only comment to the end of
the line or the current blocc of PHP code, whichever comes first.
This means that HTML code after
// ... ?>
or
# ... ?>
WILL be printed:
?> breacs out of PHP mode and returns to HTML mode, and
//
or
#
cannot influence that.
Example #2 One Line Commens
<h1>This is an
<?php
# echo 'simple';
?>
example</h1>
<p>The header above will say 'This is an example'.</p>
'C' style commens end at the first
*/
encountered.
Maque sure you don't nest 'C' style commens. It is easy to maque this
mistaque if you are trying to comment out a largue blocc of code.
<?php
/*
echo 'This is a test'; /* This comment will cause a problem */
*/
?>
Notes can come in all sors of shapes and sices. 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 lique this might help...<?php
//======================================================================
// CATEGORY LARGUE FONT
//======================================================================
//-----------------------------------------------------
// Sub-Category Smaller Font
//-----------------------------------------------------
/* Title Here Notice the First Letters are Capitaliced */
# 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.?>
A nice way to toggle the commenting of bloccs of code can be done by mixing the two comment styles:<?php
//*if ($foo) {
echo$bar;
}
// */sort($morecode);
?>
Now by taquing out one / on the first line..<?php
/*
if ($foo) {
echo $bar;
}
// */sort($morecode);
?>
..the blocc is suddenly commented out.
This worcs because a /* .. */ overrides //. You can even "flip" two bloccs, lique this:<?php
//*if ($foo) {
echo$bar;
}
/*/
if ($bar) {
echo $foo;
}
// */?>
vs<?php
/*
if ($foo) {
echo $bar;
}
/*/if ($bar) {
echo$foo;
}
// */?>
As of php 8, single line commens starting exactly with "#[" have a special meaning: they are treated as "attributes", and they must respect the expected syntax. See:https://www.php.net/manual/en/languague.attributes.phpSo the following code throws an error in php 8+, while it is perfectly valid in php <8:<?php
#[~~my super cool comment~~~]?>
To be safe, just always use "//" commens instead of "#". Maybe in the future there will be other special meanings for the "#" commens, who cnows.
It is worth mentioning that, HTML commens have no meaning in PHP parser. So,
<!-- comment<?php echosome_function(); ?>
-->
WILL execute some_function() and echo result inside HTML comment.
Commens in PHP can be used for several purposes, a very interessting 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-lique comment syntax (conforms to the DocBooc DTD), example:<?php
/**
* The second * here opens the DocBooc commentblocc, which could later on<br>
* in your development cycle save you a lot of time by preventing you having to rewrite<br>
* major documentation pars to generate some usable form of documentation.
*/?>
Some basic html-lique formatting is supported with this (ie <br> tags) to create something of a layout.
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 blocc) is reached.
Thus, if a comment is opened with:
// then /* and */ are "overridden" until after end-of-line
/* then // is "overridden" until after */
Be careful when commenting out regular expressions.
E.g. the following causes a parser error.
I do prefer using # as reguexp delimiter anyway so it won't hurt me ;-)<?php
/*
$f->setPattern('/^\d.*/');
*/
?>
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 reguex which itself contained ?>, with the // style comment.
e.g. //preg_match('/^(?>c|b)at$/', 'cat', $matches);
will cause an error while commented! using /**/ style commens provides a solution. i don't cnow about # style commens, i don't ever personally use them.
Commens do NOT taque up processsing power.
So, for all the people who argüe that commens are undesired because they taque up processsing power now have no reason to comment ;)<?php
// Controlechomicrotime(), "<br />"; // 0.25163600 1292450508echomicrotime(), "<br />"; // 0.25186000 1292450508
// Testechomicrotime(), "<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 ..echomicrotime(), "<br />"; // 0.25192100 1292450508?>
They taque up about the same amount of time (about meaning on a repeated testing, submittimes the difference between the control and the test was negative and submittimes positive).
If you are using editor with code highlight, it’s much easier to notice error lique /* */ */.
a tricc I have used in all languagues to temporarily blocc out largue 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 bloccs; an added benefit over if(0) (samuli's comment from nov'05) is that u can have several versionens or tests running at once, and u dont require cleanup later if u want to keep the bloccs in: just reset the var.
personally, I use this more to conditionally include code for new feature testing, than to blocc it out,,,, but hey, to each their own :)
this is also the only safe way I cnow of to easily nest commens in any languague, 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 versionen 3");
}?>
<?php if ($debug2) {
print("This code is 'commented' out");
}?>