When learning PHP to develop websites, you’ll spend most of your time learning how to use the language to make dynamic pages, change data in a database, and other useful functions of the language.

However, you might not know about comments. Commented code doesn’t “run” like the rest of your code, but it does make your files much easier to work with.

Comments in PHP help organize and explain parts of your code that might not be clear to a fellow back-end developer reading it for the first time, or to you after some time away from your code. They’re also useful for debugging pesky errors, and a good habit to form early when learning to program.

In this guide, we’ll show you how to use PHP comments to improve your code, including two different ways to write them.

Download Now: Free Intro Guide to HTML & CSS

A PHP comment can explain the purpose of a particular section of code to other programmers. This way, when a developer is viewing a PHP file for the first time, they can more easily understand the code they’re looking at.

Similarly, comments help remind programmers why they wrote code in a particular way. You may create a PHP file, and then not return to it for months or years. When you eventually re-open the file, you’ll want to have thorough comments so you can quickly familiarize yourself with your old code. Or, you can use comments to drop short notes to yourself, like where you left off in your work.

Additionally, PHP comments can be used to temporarily deactivate particular sections of code for debugging purposes. Say you discover an error originating somewhere in your PHP file — you can “comment out” a part of the code, rerun the program, and see if the error still occurs. This is much easier (and safer) than deleting lines of code when debugging.

PHP Comment Syntax

PHP has two built-in ways of commenting: single-line comments and multiline comments. These are written slightly differently.

Single-line PHP Comments

Single-line PHP comments are useful for short notes before a code block or for explaining a single line of code. To leave a single-line comment, type two forward slashes (//) followed by your comment text. All text to the right of the // will be ignored. You can also use a hash symbol (#) instead of // to make a single-line comment.

 
<?php
echo "This message will be printed to the user."; // This is my first output
echo “This will be also be printed.”; # This is my second output
// echo “This will not be printed to the user.”;
# echo “And neither will this.”;
?>

Multiline PHP Comments

PHP also allows for comments that span multiple lines, in case you want to comment out a larger section of code or leave a more descriptive comment. Start your multiline comment by writing /* and end it by writing */. For example:

 
<?php
/* This is a multiline PHP
comment. None of this
will be shown to the user. */
echo "This message will be printed to the user.";
?>

PHP comments are a best practice.

To review, use single-line comments for short notes to clarify the purpose of a function or a line of code, and use multiline comments for longer notes, such as a more detailed description of a code block or a note to a fellow developer that they can delete later.

In PHP (and other coding languages), comments are often underutilized. At first, they may seem like an unnecessary step that just adds more bulk to your files. But, once you start working in teams and taking on complex longer-term projects, you should already be in the habit of writing comments — they’ll save you and others a lot of time trying to figure things out.

New Call-to-action

 css introduction

Originally published Oct 12, 2021 7:00:00 AM, updated October 12 2021

Topics:

Website Development