当学习PHP到开发网站,您将花费大部分时间学习如何使用语言制作动态页面,更改数据库中的数据以及该语言的其他有用功能。

但是,您可能不知道评论。注释的代码不会像您的其余代码那样“运行”,但是它确实使您的文件更易于使用。

PHP中的评论有助于组织和解释代码的一部分,而这些部分可能尚不清楚。后端开发人员第一次阅读它,或者在远离代码之后给您。它们也可用于调试讨厌的错误,也是一个很好的习惯学习编程

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

立即下载: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.

同样,评论有助于提醒程序员为什么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.

此外,PHP注释可用于临时停用代码的特定部分,以进行调试。假设您发现了源自PHP文件中某个地方的错误 - 您可以“评论”代码的一部分,重新​​运行程序,并查看是否仍会发生错误。与调试时删除代码行相比,这要容易得多(更安全)。

PHP评论语法

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

单线PHP评论

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 (//),然后是您的评论文字。所有文本都位于//will be ignored. You can also use a hash symbol () 代替//发表单线评论。


<?php
回声“此消息将印刷给用户。”;//这是我的第一个输出
echo “This will be also be printed.”; # This is my second output
//回声“这不会给用户打印。”;
#echo “And neither will this.”;
?>

Multiline PHP Comments

PHP还允许发表跨越多行的注释,以防您要评论更大的代码部分或留下更具描述性的评论。通过写作开始您的多琳评论/*and end it by writing*/。For example:


<?php
/*这是多行PHP
评论。这一切都没有
将显示给用户。*/
回声“此消息将印刷给用户。”;
?>

PHP comments are a best practice.

要查看,请使用单行注释作为简短说明来澄清函数或一系列代码的目的,并使用多行注释较长的注释,例如对代码块的更详细说明或向其他开发人员的注释,他们以后可以删除。

在PHP(和其他编码语言)中,评论通常被低估了。起初,它们似乎是一个不必要的步骤,只会在您的文件中增加更多的体积。但是,一旦您开始在团队中工作并进行复杂的长期项目,您就应该习惯写评论 - 他们将为您和其他人节省很多时间试图解决问题。

New Call-to-action

css introduction

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

话题:

网站开发