BCA / B.Tech 11 min read

Comments in PHP

Quick Summary & Key Takeaways

  • • Subject Focus: PHP Notes in English (BCA / B.Tech Syllabus)
  • • Core Topic: Comments in PHP - General Topics
  • • Overview: Comments in PHP in Hindi | Comments in PHP in Hindi:Comments in PHP help in improving and understanding the code. This not only keeps the code organized...
  • • Key Concepts: Comments in PHP, Single line multi line docblocks Hindi, Comments best practices,Comments in PHP, Single line multi line docblocks, Comments best practices

Comments in PHP in Hindi | Comments in PHP in Hindi:


  • Comments in PHP help in improving and understanding the code. This not only keeps the code organized but also makes communication between developers easy.
  • By using single-line, multi-line, and documentation comments in PHP, you can make your code clear, structured, and easy to understand.
  • When we write code in PHP, sometimes we need to write comments to explain our code or to remember it later.
  • Comments are that part of the code that the program ignores while running.
Why are Comments used in PHP?

  • To explain the code: When other developers read your code, they can understand what you have written and why.
  • To make notes for the future: If you work on your code later, you can understand your own code quickly.
  • To temporarily disable code: To temporarily disable some parts of the code.
Types of Comments in PHP in Hindi | Types of Comments in PHP:

There are mainly three types of comments in PHP:

  • Single-line Comment
  • Multi-line Comment
  • PHPDoc Comment (Documentation Comment)

1. Single-line Comment: A single-line comment is written in a single line. It can be written in two ways:

// (double slash)
# (hash)

Example:

<?php
// This is a single-line comment
echo "Hello World!"; // This is also a comment

# This is also another way of a single-line comment
echo "Learning PHP is easy!";
?>

2. Multi-line Comment: A multi-line comment is used when we need to write a comment in multiple lines. It is written between /* and */.

Example:

<?php
/* 
This is a multi-line comment.
It is used when
the comment is in multiple lines.
*/
echo "Commenting in PHP is easy!";
?>

3. PHPDoc Comment (Documentation Comment): A PHPDoc comment is used to give detailed information about a function, class, or a large code block. It is written between /** and */.
This is especially useful for documentation tools (like PHPDocumentor).

Example:

<?php
/**
 * This function adds two numbers.
 *
 * @param int $a The first number
 * @param int $b The second number
 * @return int The sum of both numbers
 */
function addNumbers($a, $b) {
    return $a + $b;
}

echo addNumbers(5, 10); // Will print 15
?>

Advantages of using Comments in PHP in Hindi | Advantages of using Comments:

  • Makes the code easy to understand: When other developers work on your code, they can understand the code quickly with the help of comments.
  • Helps in finding bugs: If there is any problem in the code, it can be found quickly by looking at the comments.
  • Helpful in working in a group: When many people work on the same project, it becomes easy to understand each other's code with comments.
  • Project documentation: It becomes easy to create automatic documentation of the code from PHPDoc comments.

Right use of Comments in PHP in Hindi | Correct use of Comments:

  • Write comments only in necessary places.
  • Avoid too much or too little commenting.
  • Use comments to clarify the purpose and logic of the code.
  • Always keep comments simple and clear.
  • Mistakes that should not be made while writing Comments
  • Unnecessary comments on clear code: If the code itself is easy to understand, there is no need to write comments there.

$x = 10; // I am setting x to 10 (unnecessary)

Removing old comments: If the code has changed, it is important to update or remove old comments.
Long comments: Write short and precise comments.

Real Use of Comments in PHP in Hindi | Real use of Comments in PHP:

<?php
// Code to connect to the database
$servername = "localhost";
$username = "root";
$password = "";
$database = "my_database";

# Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

/*
The line below is checking the connection.
If the connection fails, an error will be shown.
*/
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connection successful!";

/**
 * This function prints the user's information.
 *
 * @param string $name The user's name
 * @param int $age The user's age
 */
function printUserInfo($name, $age) {
    echo "User's name: $name, age: $age";
}

printUserInfo("Rahul", 25); // Will print user's information
?>

Frequently Asked Questions (FAQs)

Comments in PHP in Hindi | Comments in PHP in Hindi:Comments in PHP help in improving and understanding the code.
Understanding Comments in PHP is fundamental to mastering PHP Notes in English. It covers crucial concepts, solved problems, and revision points essential for academic and entrance exam success.
You can read free solved examples, detailed explanations, and key formulas for Comments in PHP directly on the NotesMedia website or by downloading the NotesMedia App.