comment in php code example
Example 1: comment in php
<?php
*/
?>
Example 2: comment in php
<?php
$var = 2 + 2;
echo $var;
?>
Example 3: comment in php
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
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;
}
?>
vs
<?php
if ($bar) {
echo $foo;
}
?>
Example 4: comment in php
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
?>
Example 5: comment in php
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.*/');
*/
?>
Example 6: comment in php
It is worth mentioning that, HTML comments have no meaning in PHP parser. So,
WILL execute some_function() and echo result inside HTML comment.
Example 7: comment in php
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
?>
Some basic html-like formatting is supported with this (ie <br> tags) to create something of a layout.
Example 8: comment in php
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
echo microtime(), "<br />";
echo microtime(), "<br />";
echo microtime(), "<br />";
echo microtime(), "<br />";
?>
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).
Example 9: comment in php
<?php
echo 'This is a test';
echo 'This is yet another test';
echo 'One Final Test';
?>
Example 10: comment in php
<h1>This is an <?php ?> example</h1>
<p>The header above will say 'This is an example'.</p>