When learning PHP to develop websites, you’ll spend most of your time learning how to lớn use the language to make dynamic pages, change data in a database, & other useful functions of the language.
Bạn đang xem: Code bình luận php
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 khổng lồ work with.
Comments in PHP help organize and explain parts of your code that might not be clear lớn a fellow back-end developer reading it for the first time, or khổng lồ you after some time away from your code. They’re also useful for debugging pesky errors, and a good habit to size early when learning to lớn program.
In this guide, we’ll show you how khổng lồ use PHP comments to improve your code, including two different ways to lớn write them.

What are PHP comments?
In PHP, comments are sections of code that are not executed when a program is run. Kết thúc users bởi vì not see PHP comments — they are only visible when viewing the PHP code in a file. PHP comments are usually meant to lớn help programmers understand và interpret the PHP code.
A PHP comment can explain the purpose of a particular section of code to lớn 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, & then not return lớn 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 lớn yourself, lượt thích where you left off in your work.
Xem thêm: Top 3 Phần Mềm Webcam Cho Pc, Laptop, Cyberlink Youcam
Additionally, PHP comments can be used lớn temporarily deactivate particular sections of code for debugging purposes. Say you discover an error originating somewhere in your PHP tệp tin — you can “comment out” a part of the code, rerun the program, & see if the error still occurs. This is much easier (and safer) than deleting lines of code when debugging.
PHP bình luận 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 bình luận text. All text lớn the right of the // will be ignored. You can also use a hash symbol (#) instead of // to make a single-line comment.
echo "This message will be printed to the user."; // This is my first outputecho “This will be also be printed.”; # This is my second output// echo “This will not be printed lớn the user.”;# echo “And neither will this.”;?>
Multiline PHP Comments
PHP also allows for comments that span multiple lines, in case you want to phản hồi out a larger section of code or leave a more descriptive comment. Start your multiline bình luận by writing /* and end it by writing */. For example:
/* This is a multiline PHPcomment. None of thiswill be shown to the user. */echo "This message will be printed lớn the user.";?>
PHP comments are a best practice.
To review, use single-line comments for short notes lớn clarify the purpose of a function or a line of code, and use multiline comments for longer notes, such as a more detailed mô tả tìm kiếm of a code block or a cảnh báo 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 khổng lồ 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 và others a lot of time trying khổng lồ figure things out.