Can you comment through multiple lines in php?

This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0

This website is not affiliated with Stack Overflow

Can you comment through multiple lines in php?

SUPPORT & PARTNERS

  • Advertise with us
  • Contact us
  • Privacy Policy

STAY CONNECTED

Get monthly updates about new articles, cheatsheets, and tricks.

Can you comment through multiple lines in php?

Using Comments in PHP

Comments in PHP are similar to comments that are used in HTML. The PHP comment syntax always begins with a special character sequence and all text that appears between the start of the comment and the end will be ignored.

In HTML a comment's main purpose is to serve as a note to you, the web developer or to others who may view your website's source code. However, PHP's comments are different in that they will not be displayed to your visitors. The only way to view PHP comments is to open the PHP file for editing. This makes PHP comments only useful to PHP programmers.

In case you forgot what an HTML comment looked like, see our example below.

HTML Code:

PHP Comment Syntax: Single Line Comment

While there is only one type of comment in HTML, PHP has two types. The first type we will discuss is the single line comment. The single line comment tells the interpreter to ignore everything that occurs on that line to the right of the comment. To do a single line comment type "//" or "#" and all text to the right will be ignored by PHP interpreter.

PHP Code:

Psst...You can't see my PHP comments!"; // echo "nothing";
// echo "My name is Humperdinkle!";
# echo "I don't do anything either";
?>

Display:

Hello World!
Psst...You can't see my PHP comments!

Notice that a couple of our echo statements were not evaluated because we commented them out with the single line comment. This type of line commenting is often used for quick notes about complex and confusing code or to temporarily remove a line of PHP code.

PHP Comment Syntax: Multiple Line Comment

Similiar to the HTML comment, the multi-line PHP comment can be used to comment out large blocks of code or writing multiple line comments. The multiple line PHP comment begins with " /* " and ends with " */ ".

PHP Code:

Display:

Hello World!

Good Commenting Practices

One of the best commenting practices that I can recommend to new PHP programmers is....USE THEM!! So many people write complex PHP code and are either too lazy to write good comments or believe the commenting is not needed. However, do you really believe that you will remember exactly what you were thinking when looking at this code a year or more down the road?

Let the comments permeate your code and you will be a happier PHPer in the future. Use single line comments for quick notes about a tricky part in your code and use multiple line comments when you need to describe something in greater depth than a simple note.

Can you comment through multiple lines in php?

  • Go Back
  • Continue

Download Tizag.com's PHP Book

If you would rather download the PDF of this tutorial, check out our PHP eBook from the Tizag.com store. Print it out, write all over it, post your favorite lessons all over your wall!

Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

Summary: in this tutorial, you’ll learn how to use PHP comments to document your code.

Comments are important parts of the code. Comments provide useful information that will help you and other developers understand the meaning of the code more quickly later.

PHP supports two types of comments:

  • One-line comments
  • Multi-line comments

One-line comments

The one-line comment is placed at the end of the line or at the current block.

A one-line comment starts with the pound (#) or double forward-slash (//). The rest of the text after the (//) is ignored by the PHP interpreter.

The following example uses the // for a one-line comment:

$rate = 100; $hours = 173; $payout = $hours * $rate; // payout calculation

Code language: HTML, XML (xml)

And the following example uses the # for a one-line comment:

$title = 'PHP comment'; # set default title

Code language: HTML, XML (xml)

Multi-line comments

A Multi-line comment start with /* and end with */. For example:

/* This is an example of a multi-line comment, which can span multiple lines. */

Code language: HTML, XML (xml)

In practice, you use the multi-line comment when you need to span comments multiple lines.

Writing meaningful comments

To document your code effectively, you use the following guidelines:

1) Making the code speak for itself without using comments by naming meaningful identifiers. For example, you can use the following:

$is_completed = true;

Code language: PHP (php)

Instead of using a cryptic name with a comment:

$ic = true; // is completed

Code language: PHP (php)

The code itself can be good comments.

2) Don’t write a comment to explain what code does, instead, explain why it does so. For example:

// complete the task $is_completed = true

Code language: PHP (php)

3) When writing a comment, make it as concise as possible.

Summary

  • Comments are important parts of the code because they explain why code does what it is supposed to do.
  • PHP supports both one-line and multi-line comments.
  • A one-line comment starts with the # or // .
  • A multi-line comment starts with /* and end with */.

Did you find this tutorial useful?

How do you comment out a multiple lines of PHP codes?

PHP Comments.
Syntax for single-line comments: // This is a single-line comment. ... .
Syntax for multiple-line comments: /* ... .
Using comments to leave out parts of the code: // You can also use comments to leave out parts of a code line..

Is it used to add multiple line comments in PHP?

Multi-line Comments PHP multi-line line comment begins with /* , and ends with */ .

How do you comment a whole section in PHP?

PHP supports both one-line and multi-line comments. A one-line comment starts with the # or // . A multi-line comment starts with /* and end with */ .

How do you comment multiple lines at once?

To comment out multiple code lines right-click and select Source > Add Block Comment. ( CTRL+SHIFT+/ )