WikiGalaxy

Personalize

PHP Comments

Single-line Comments:

Single-line comments in PHP are created using two forward slashes (//) or a hash (#). These comments extend to the end of the line.


<?php
// This is a single-line comment
echo "Hello, World!";
# Another single-line comment
?>
      

Multi-line Comments

Multi-line Comments:

Multi-line comments are enclosed within /* and */. These comments can span multiple lines.


<?php
/*
This is a multi-line comment.
It can span multiple lines.
*/
echo "Multi-line comments in PHP!";
?>
      

Inline Comments

Inline Comments:

Inline comments are placed on the same line as a statement, separated by a space.


<?php
echo "Inline comments"; // This is an inline comment
echo "Another example"; # Another inline comment
?>
      

Commenting Best Practices

Best Practices:

Use comments to explain complex logic, mark sections of code, and provide context for future reference.


<?php
// Calculate the sum of two numbers
$sum = $a + $b; // $a and $b are inputs
echo $sum; // Output the result
?>
      

Avoiding Over-commenting

Avoid Over-commenting:

While comments are helpful, over-commenting can clutter the code. Only comment on non-obvious parts of the code.


<?php
// Set variable to 5
$x = 5; // This comment is unnecessary
?>
      

Using DocBlocks

DocBlock Comments:

DocBlocks are used for documenting code elements like classes, functions, and files. They start with /** and end with */.


<?php
/**
 * Calculate the area of a rectangle.
 *
 * @param float $length
 * @param float $width
 * @return float
 */
function calculateArea($length, $width) {
    return $length * $width;
}
?>
      

Commenting Out Code

Commenting Out Code:

Temporarily disable parts of code using comments. This is useful for debugging or testing purposes.


<?php
// echo "This line is commented out and won't execute.";
echo "This line will execute.";
?>
      

Using Comments for Version Control

Version Control Comments:

Comments can be used to track changes and versions of code, especially when collaborating with others.


<?php
// Version 1.0 - Initial Release
// Version 1.1 - Bug fixes and improvements
echo "Version control using comments.";
?>
      
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025