1
2A nice way to toggle the commenting of blocks of code can be done by mixing the two comment styles:
3<?php
4//*
5if ($foo) {
6 echo $bar;
7}
8// */
9sort($morecode);
10?>
11
12Now by taking out one / on the first line..
13
14<?php
15/*
16if ($foo) {
17 echo $bar;
18}
19// */
20sort($morecode);
21?>
22..the block is suddenly commented out.
23This works because a /* .. */ overrides //. You can even "flip" two blocks, like this:
24<?php
25//*
26if ($foo) {
27 echo $bar;
28}
29/*/
30if ($bar) {
31 echo $foo;
32}
33// */
34?>
35vs
36<?php
37/*
38if ($foo) {
39 echo $bar;
40}
41/*/
42if ($bar) {
43 echo $foo;
44}
45// */
46?>
47
48
1
2<?php
3 echo 'This is a test'; // This is a one-line c++ style comment
4 /* This is a multi line comment
5 yet another line of comment */
6 echo 'This is yet another test';
7 echo 'One Final Test'; # This is a one-line shell-style comment
8?>
9
10
1
2<h1>This is an <?php # echo 'simple';?> example</h1>
3<p>The header above will say 'This is an example'.</p>
4
1
2Comments do NOT take up processing power.
3
4So, for all the people who argue that comments are undesired because they take up processing power now have no reason to comment ;)
5
6<?php
7
8// Control
9echo microtime(), "<br />"; // 0.25163600 1292450508
10echo microtime(), "<br />"; // 0.25186000 1292450508
11
12// Test
13echo microtime(), "<br />"; // 0.25189700 1292450508
14# TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST
15# .. Above comment repeated 18809 times ..
16echo microtime(), "<br />"; // 0.25192100 1292450508
17
18?>
19
20They 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).
21
1
2Comments 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/).
3
4Therefor one has to use a JavaDoc-like comment syntax (conforms to the DocBook DTD), example:
5<?php
6/**
7* The second * here opens the DocBook commentblock, which could later on<br>
8* in your development cycle save you a lot of time by preventing you having to rewrite<br>
9* major documentation parts to generate some usable form of documentation.
10*/
11?>
12Some basic html-like formatting is supported with this (ie <br> tags) to create something of a layout.
13
1
2Be careful when commenting out regular expressions.
3
4E.g. the following causes a parser error.
5
6I do prefer using # as regexp delimiter anyway so it won't hurt me ;-)
7
8<?php
9
10/*
11
12 $f->setPattern('/^\d.*/');
13
14*/
15
16?>
17
18
1
2Notes 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...
3
4<?php
5
6//======================================================================
7// CATEGORY LARGE FONT
8//======================================================================
9
10//-----------------------------------------------------
11// Sub-Category Smaller Font
12//-----------------------------------------------------
13
14/* Title Here Notice the First Letters are Capitalized */
15
16# Option 1
17# Option 2
18# Option 3
19
20/*
21 * This is a detailed explanation
22 * of something that should require
23 * several paragraphs of information.
24 */
25
26// This is a single line quote.
27?>
28
29
1
2MSpreij (8-May-2005) says /* .. */ overrides //
3Anonymous (26-Jan-2006) says // overrides /* .. */
4
5Actually, 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.
6
7Thus, if a comment is opened with:
8 // then /* and */ are "overridden" until after end-of-line
9 /* then // is "overridden" until after */
10
1
2it'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 ?>
3
4<?php
5if(1==1)
6{
7 //?>
8}
9?>
10
11i discovered this "anomally" when i commented out a line of code containing a regex which itself contained ?>, with the // style comment.
12e.g. //preg_match('/^(?>c|b)at$/', 'cat', $matches);
13will 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.
14