Home  >  Article  >  Backend Development  >  What are the standard annotation methods in PHP?

What are the standard annotation methods in PHP?

little bottle
little bottleOriginal
2019-04-17 17:57:374110browse


This article introduces six PHP annotation methods. Let’s learn them together!

PHP single-line comment syntax

All text to the right of the DE>//DE> symbol in a line is considered a comment, because PHP The parser ignores this line DE>//DE> Everything to the right. As follows:

<span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;"><?php<br/>echo "Blabla.cn"; // 这是单行注释?><br/></span>

You can also write like this, only write comments in one line, no code, as follows:

<span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;"><?php// 这是单行注释echo "Blabla.cn"; <br/>// 这是单行注释?><br/></span>

PHP multi-line comment syntax

PHP multi-line comments start with DE>/*DE> and end with DE>*/DE> End. Between DE>/*DE> and DE>*/DE>, you can write multiple lines of comments.

The example is as follows, the red part is the content of multi-line comments.

<span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;"><?php<br/>echo "Blabla.cn";/*<br/>这是多行注释<br/>这是多行注释<br/>*/?><br/></span>

Block comments

Block comments are typically used to provide descriptions of files, methods, data structures, and algorithms. Block comments are placed at the beginning of every file and before every method. They can also be used elsewhere, such as inside methods. Block comments inside functions and methods should have the same indentation as the code they describe.

There should be a blank line at the beginning of the block comment to separate the block comment from the code, for example:
##

/*
 
* 这里是块注释
*/

Block comments can start with /*- so that indent(1) can recognize it as a code the beginning of the block without reordering it.


##

/*-
 
* 如果想被忽略,可是使用特别格式的块注释
*
* one
*   two
*     three
*/

Note: If you don’t use indent (1), you don't have to use /*- in your code, or give in to the possibility that others will run indent(1) on your code. Single-line comments
Short comments can appear on one line and have the same indentation level as the following code. If a comment cannot be written in one line, use a block comment. Single-line comments should be preceded by a blank line. Here is an example of a single line comment in the code:



##

if (condition) {
 
/* 以下代码运行的条件 */
 
...
}

Tail commentsVery short comments can be on the same line as the code they describe, but should be separated by enough whitespace Code and comments. If multiple short comments appear in a large block of code, they should have the same indentation. The following is an example of a trailing comment in the code:



if ($a == 2) {
 
return TRUE; /* 对单一条件的说明 */
} else {
return isPrime($a); /* 其余的条件 */
}

行末注释

注释界定符"//",可以注释掉整行或者一行中的一部分。它一般不用于连续多行的注释文本;然而,它可以用来注释掉连续多行的代码段。以下是所有三种风格的例子:


if ($foo > 1) {
 
// 第二种用法.
 
...
}
else {
return false; // 说明返回值的原因
}
//if ($bar > 1) {
 
//
//  // 第三种用法
//  ...
//}
//else {
// return false;
//}


文档注释

文档注释描述php的类、构造器,方法,以及字段(field)。每个文档注释都会被置于注释定界符/**...*/之中,一个注释对应一个类或成员。该注释应位于声明之前:


/**
 
* 说明这个类的一些 ...
*/
class Example { ...


注意

顶层(top-level)的类是不缩进的,而其成员是缩进的。

描述类的文档注释的第一行(/**)不需缩进;随后的文档注释每行都缩进1格(使星号纵向对齐)。

成员,包括构造函数在内,其文档注释的第一行缩进4格,随后每行都缩进5格。

文档注释不能放在一个方法或构造器的定义块中,因为程序会将位于文档注释之后的第一个声明与其相关联。

【相关课程:PHP视频教程

The above is the detailed content of What are the standard annotation methods in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn