我可以在 PHP 中使用井号 (#) 进行注释吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9093609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use a hash sign (#) for commenting in PHP?
提问by Hubro
I have never, ever, seen a PHP file using hashes (#
) for commenting. But today I realized that I actually can! I'm assuming there's a reason why everybody uses //
instead though, so here I am.
我从未见过使用哈希 ( #
) 进行注释的 PHP 文件。但是今天我意识到我真的可以!我假设每个人都使用它是有原因的//
,所以我在这里。
Is there any reason, aside from personal preference, to use //
rather than #
for comments?
除了个人喜好之外,是否有任何理由使用//
而不是#
评论?
回答by Aziz
The answer to the question Is there any differencebetween using "#" and "//" for single-line comments in PHP?is no.
该问题的答案是否有任何区别使用“#”和“//”在PHP单行注释之间?是没有。
There is no difference. By looking at the parsing part of PHP source code, both "#" and "//" are handled by the same codeand therefore have the exact same behavior.
没有区别。通过查看 PHP 源代码的解析部分,“#”和“//”都由相同的代码处理,因此具有完全相同的行为。
回答by naitsirch
PHP's documentation describes the different possibilities of comments. See http://www.php.net/manual/en/language.basic-syntax.comments.php
PHP 的文档描述了注释的不同可能性。见http://www.php.net/manual/en/language.basic-syntax.comments.php
But it does not say anything about differences between "//" and "#". So there should not be a technical difference. PHP uses C syntax, so I think that is the reason why most of the programmers are using the C-style comments '//'.
但它没有说明“//”和“#”之间的区别。所以不应该有技术差异。PHP 使用 C 语法,所以我认为这就是为什么大多数程序员使用 C 风格的注释“//”的原因。
回答by ajreal
回答by Sithu
Is there any reason, aside from personal preference, to use // rather than # for comments?
除了个人喜好之外,是否有任何理由使用 // 而不是 # 进行评论?
I think it is just a personal preference only. There is no difference between //
and #
. I personally use #
for one-line comment, //
for commenting out code and /** */
for block comment.
我认为这只是个人喜好而已。//
和之间没有区别#
。我个人#
用于单行注释、//
注释代码和/** */
块注释。
<?php
# This is a one-line comment
echo 'This is a test';
// echo 'This is yet another test'; // commenting code
/**
* This is a block comment
* with multi-lines
*/
echo 'One final test';
?>
回答by Brandin
One might thinkthat the #
form of commenting is primarily intended to make a shell script using the familiar "shebang" (#!) notation. In the following script, PHP should ignore the first line because it is also a comment. Example:
人们可能认为#
注释的形式主要是为了使用熟悉的“shebang”(#!) 符号制作一个 shell 脚本。在下面的脚本中,PHP 应该忽略第一行,因为它也是一个注释。例子:
#!/usr/bin/php
<?php
echo "Hello PHP\n";
If you store it in an executable file you can then run it from a terminal like this
如果将其存储在可执行文件中,则可以从这样的终端运行它
./hello
The output is
输出是
Hello PHP
However, this reasoning is incorrect, as the following counterexample shows:
但是,这种推理是不正确的,如下面的反例所示:
#!/usr/bin/php
#A
<?php
#B
echo "Hello PHP\n";
The first line (the shebang line) is specially ignored by the interpreter. The comment line before the PHP tag is echoed to standard output because it is not inside a PHP tag. The comment after the opening PHP tag is interpreted as PHP code but it is ignored because it is a comment.
解释器会特别忽略第一行(shebang 行)。PHP 标记之前的注释行会回显到标准输出中,因为它不在 PHP 标记内。PHP 开头标记后的注释被解释为 PHP 代码,但由于它是注释而被忽略。
The output of the revised version is
修订版的输出是
#A
Hello PHP
回答by d.raev
If you establish some rule sets in your team / project... the 2 types of comments can be used to outline the purpose of the commented code.
如果您在您的团队/项目中建立了一些规则集……这两种类型的注释可用于概述注释代码的用途。
For example I like to use #
to mute / disable config settings, sub functions and in general a piece of code that is useful or important, but is just currently disabled.
例如,我喜欢用来#
静音/禁用配置设置、子功能以及一般有用或重要但目前仅被禁用的一段代码。
回答by Lucas Bustamante
There's no official PSR for that.
没有官方的 PSR。
However, in all PSR example code, they use //
for inline comments.
但是,在所有 PSR 示例代码中,它们都//
用于内联注释。
There's an PSR-2 extension proposal that aims to standardize it, but it's not official: https://github.com/php-fig-rectified/fig-rectified-standards/blob/master/PSR-2-R-coding-style-guide-additions.md#commenting-code
有一个旨在标准化它的 PSR-2 扩展提案,但它不是官方的:https: //github.com/php-fig-rectified/fig-rectified-standards/blob/master/PSR-2-R-coding- style-guide-additions.md#commenting-code
//
is more commonly used in the PHP culture, but it's fine to use #
too. I personally like it, for being shorter and saving bytes. It's personal taste and biased, there's no right answer for it, until, of course, it becomes a standard, which is something we should try to follow as much as possible.
//
在 PHP 文化中更常用,但也可以使用#
。我个人喜欢它,因为它更短并且节省了字节。这是个人品味和偏见,没有正确的答案,当然,直到它成为一个标准,这是我们应该尽可能多地遵循的标准。
回答by Mark N Hopgood
Yes, however there are cross platform differences.
是的,但是存在跨平台差异。
I use # all the time for commenting in PHP, but I have noticed an adoption difference.
我一直使用 # 在 PHP 中进行评论,但我注意到采用差异。
On windows keyboard the # key is easy to use. On mac keyboard # key mostly isn't present.
在 Windows 键盘上,# 键很容易使用。在 mac 键盘上,# 键大多不存在。
So for mac users, [Alt] + [3] or [?] + [3] is more difficult to type than //, so // has become a cross platform way of displaying code with comments.
所以对于mac用户来说,[Alt]+[3]或者[?]+[3]比//更难打字,所以//已经成为一种跨平台的带注释的代码展示方式。
This is my observation.
这是我的观察。
回答by BarbaraRoseNow
From https://php.net/manual/en/migration53.deprecated.php
来自https://php.net/manual/en/migration53.deprecated.php
"Deprecated features in PHP 5.3.x ...Comments starting with '#' are now deprecated in .INI files."
“PHP 5.3.x 中已弃用的功能......以 '#' 开头的注释现在在 .INI 文件中已弃用。”
There you have it. Hash '#' appears to remain as a comment option by default by not being deprecated. I plan to use it to distinguish various layers of nested if/else statements and mark their closing brackets, or use to distinguish code comments from commented out code as others have suggested in related posts. (Note: Link was valid/working as of 4/23/19, although who knows if it'll still be working when you're reading this.)
你有它。默认情况下,哈希“#”似乎保留为注释选项,但未被弃用。我打算用它来区分嵌套的 if/else 语句的各个层并标记它们的右括号,或者用它来区分代码注释和注释掉的代码,正如其他人在相关帖子中所建议的那样。(注意:链接自 19 年 4 月 23 日起有效/有效,但谁知道在您阅读本文时它是否仍然有效。)
回答by Gerard ONeill
Is there any reason, aside from personal preference, to use // rather than # for comments?
除了个人喜好之外,是否有任何理由使用 // 而不是 # 进行评论?
I came here for the answer myself, and its good to know there is NOcode difference.
我自己来这里是为了答案,很高兴知道没有代码差异。
However, preference-wise one could argue that you'd prefer the 'shell->perl->php' comment consistency vs the 'c->php' way.
但是,偏好明智的人可能会争辩说,与“c->php”方式相比,您更喜欢“shell->perl->php”注释一致性。
Since I did approach php as a poor man's webby perl, I was using #.. and then I saw someone else's code and came straight to SO. ;)
因为我确实将 php 作为一个穷人的 webby perl 来处理,所以我使用了 #.. 然后我看到了别人的代码并直接来到了 SO。;)