Php 简明教程
PHP - Comments
任何计算机程序(如 PHP 程序)中的注释都是一种特定的解释性文本,语言编译器/解释器会忽略它。其目的是帮助用户理解程序算法中使用的逻辑。
A comment in any computer program (such as a PHP program) is a certain explanatory text that is ignored by the language compiler/interpreter. Its purpose is to help the user understand the logic used in the program algorithm.
尽管在代码中放置注释并不是必须的,但这是强烈推荐的做法。注释也可以作为程序文档。在需要调试和修改代码时,注释也非常有用。
Although placing comments in the code is not essential, it is a highly recommended practice. The comments also serve as program documentation. Comments are also useful when the code needs to be debugged and modified.
PHP 有两种注释格式 -
There are two commenting formats in PHP −
-
Single-line Comments
-
Multi-line Comments
Single-line Comments
它们通常用于短说明或与本地代码相关的注释。PHP 使用两种符号在程序中插入单行注释。
They are generally used for short explanations or notes relevant to the local code. PHP uses two notations for inserting a single-line comment in a program.
Single-line Comments Using "
以“#”符号开头的 PHP 代码中的行被视为单行注释。
A line in PHP code starting with the "#" symbol is treated as a single-line comment.
<?php
# Single line comment starting with # symbol
echo 'Hello World';
?>
Single-line Comments Using "//"
PHP 还支持以“//”符号为单行注释的 C 样式。以双斜线符号开头的行被视为注释。
PHP also supports C style of single-line comments with "//" symbol. A line starting with double oblique symbol is treated as a comment.
<?php
// Single line comment starting with // symbol
echo 'Hello World';
?>
以符号 “#”或 “//”开头的注释不需要关闭。这些符号的作用持续到物理行的末尾。
A comment that starts with the symbol "#" or "//" need not be closed. The effect of these symbols last till the end of the physical line.
换句话说,PHP 解析器会将下一行作为 PHP 语句对待,而不是注释,即使没有注释结束标记。
In other words, the PHP parser will treat the next line as a PHP statement and not as a comment even if there is no closing comment marker.
Multi-line Comments
多行注释通常用于在必要时提供伪代码算法和更详细的说明。
Multi-line comments are generally used to provide pseudocode algorithms and more detailed explanations when necessary.
多行注释的样式与 C 中相同。嵌入在符号 “/ " and " /”内的行或多行被视为注释。
The multiline style of commenting is the same as in C. One or more lines embedded inside the "/" and "/" symbols are treated as a comment.
Example of Multi-line Comment in PHP
这是一个多行注释的示例。
Here is the example of a multi-line comment.
<?php
/* This is a multiline comment example
program to add two numbers
Variables used - $x for first number,
$y for second number */
$x=10;
$y=20;
print "Total = ". $x+$y;
?>
请注意,您甚至可以将单行放在程序中的 “/* .. /" symbols. However, if there is a "/ " 符号内,它必须有一个注释结束标记 “*/”。如果没有,将显示以下错误 -
Note that you can put even a single line inside the "/* .. /" symbols. However, if there is a "/" symbol in the program, it must have a closing end-of comment marker "*/". If not, an error will be displayed as follows −
PHP Parse error: Unterminated comment starting line 3 in /home/cg/root/65ded9eeb52fc/main.php on line 3