<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
<?php
// Author : https://www.codedweb.org/
// This is a single-line comment
# This is also a single-line comment in unix and linux
/*
This is a Multi-lines comment block
by this way you can add muliple lines on it.
lines
*/
// You can also use comments to leave out parts of a code line
$var = 2 /* + 12 */ + 2;
echo $var;
?>
/**
* Does something interesting
*
* @param Place $where Where something interesting takes place
* @param integer $repeat How many times something interesting should happen
*
* @throws Some_Exception_Class If something interesting cannot happen
* @author Monkey Coder <mcoder@facebook.com>
*
* @return Status
*/
/*There are two types of comments:
1) Spans over multiple lines - Multiple-line comment
2) Spans over one line only - Sinlge-line comment
*/
/* There are two ways to write a single-line comment:
1) Using double slash
2) Using the hash tag
*/
// This is an example of single-line comment using double slash
# This is an example of single-line comment using hash tag
/*There is only one way of using multiple line comments, startin with the
slash and asterisk respectively, and ending with asterisk and slash
respectively */
//For a single line comment use //:
//this is a comment too
//for multi-line comments use /* and */:
/* <--start of multi-line comment
this is a comment
this is a comment too (end of multi-line comment)-->*/
<?php
echo 'This is a test'; // 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';
echo 'One Final Test'; # This is a one-line shell-style comment
?>
/**
* This function compiles a message that tells you how great coffee is
*
* @param string $compliment A nice word to describe coffee
* @param integer $score A score out of 10
*/
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.
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;
}
// */
?>
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.
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.
?>
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.
$(document).ready(function(){
// When user clicks on submit comment to add comment under post
$(document).on('click', '#submit_comment', function(e) {
e.preventDefault();
var comment_text = $('#comment_text').val();
var url = $('#comment_form').attr('action');
// Stop executing if not value is entered
if (comment_text === "" ) return;
$.ajax({
url: url,
type: "POST",
data: {
comment_text: comment_text,
comment_posted: 1
},
success: function(data){
var response = JSON.parse(data);
if (data === "error") {
alert('There was an error adding comment. Please try again');
} else {
$('#comments-wrapper').prepend(response.comment)
$('#comments_count').text(response.comments_count);
$('#comment_text').val('');
}
}
});
});
// When user clicks on submit reply to add reply under comment
$(document).on('click', '.reply-btn', function(e){
e.preventDefault();
// Get the comment id from the reply button's data-id attribute
var comment_id = $(this).data('id');
// show/hide the appropriate reply form (from the reply-btn (this), go to the parent element (comment-details)
// and then its siblings which is a form element with id comment_reply_form_ + comment_id)
$(this).parent().siblings('form#comment_reply_form_' + comment_id).toggle(500);
$(document).on('click', '.submit-reply', function(e){
e.preventDefault();
// elements
var reply_textarea = $(this).siblings('textarea'); // reply textarea element
var reply_text = $(this).siblings('textarea').val();
var url = $(this).parent().attr('action');
$.ajax({
url: url,
type: "POST",
data: {
comment_id: comment_id,
reply_text: reply_text,
reply_posted: 1
},
success: function(data){
if (data === "error") {
alert('There was an error adding reply. Please try again');
} else {
$('.replies_wrapper_' + comment_id).append(data);
reply_textarea.val('');
}
}
});
});
});
});
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.*/');
*/
?>
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).
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 */