多行注释中的 PHP 多行注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6977568/
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
PHP Multiple Line Comment inside Multiple Line Comment
提问by dudeMatt
<?php
/*
/* this is a comment */
*/
?>
PHP returns "syntax error"...
Is this just a completely wrong way to use multiple line comment?
PHP 返回“语法错误”...
这只是使用多行注释的一种完全错误的方式吗?
Sometimes I need to comment out a big block of code for testing, and this block contains hundreds of lines and there are many multiple line comments inside.
有时我需要注释掉一大块代码进行测试,而这个块包含数百行,并且里面有很多多行注释。
So what's the best way to comment out this big block? besides removing it temporarily from the file?
那么注释掉这个大块的最好方法是什么?除了暂时从文件中删除它?
采纳答案by karim79
From the PHP manual:
从PHP 手册:
'C' style comments end at the first */ encountered. Make sure you don't nest 'C' style comments. It is easy to make this mistake if you are trying to comment out a large block of code.
<?php /* echo 'This is a test'; /* This comment will cause a problem */ */ ?>
'C' 风格的注释在遇到的第一个 */ 处结束。确保不要嵌套“C”风格的注释。如果您试图注释掉一大块代码,很容易犯这个错误。
<?php /* echo 'This is a test'; /* This comment will cause a problem */ */ ?>
:(
:(
回答by McKean
There's no nice way to do this so what I usually do is to just use the following workaround:
没有好的方法可以做到这一点,所以我通常只使用以下解决方法:
<?php if(false): ?>
Whatever needs to be commented out.
<?php endif; ?>
回答by kenorb
By design PHP syntax won't allow to do that.
按照设计,PHP 语法不允许这样做。
So I think the easiest way to achieve that would be to remove all /
characters followed by *
.
所以我认为最简单的方法是删除所有/
后跟*
.
In example, the following code:
例如,以下代码:
/*
/*
* Comment 1
*/
/*
* Comment 2
*/
*/
would become:
会成为:
/*
/*
* Comment 1
*
/*
* Comment 2
*
*/
回答by Joe Enos
I'd say it depends on your IDE/editor. Some IDE's have a "comment" feature, which will do single-line comments (//
) on all lines of a selected area, so you would select the whole range and click that button.
我会说这取决于您的 IDE/编辑器。某些 IDE 具有“注释”功能,它将//
对选定区域的所有行进行单行注释 ( ),因此您可以选择整个范围并单击该按钮。
If your IDE doesn't have that feature, then I think you're out of luck.
如果您的 IDE 没有该功能,那么我认为您不走运。
For example, suppose this is your original code
例如,假设这是您的原始代码
$a = 1; /* sets a = 1 */
$b = 2;
/*
blah blah
*/
If you highlight that whole thing in some IDEs and click the comment button, you'll end up with:
如果您在某些 IDE 中突出显示整个内容并单击评论按钮,您将得到:
// $a = 1; /* sets a = 1 */
// $b = 2;
// /*
// blah blah
// */
The //
comments win, which mean you just did what you're trying to accomplish.
该//
意见夺冠,这意味着你只是做了你要完成的任务。
回答by Hassan Saeed
for smart move just add and save you whole desire comment code section in "yourCodeBlock.php"then
对于聪明的举动,只需在“yourCodeBlock.php”中添加并保存您想要的全部评论代码部分,然后
<?php
/*
include("yourCodeBlock.php");
*/
?>
or simple single line comment
或简单的单行注释
<?php
//include("yourCodeBlock.php");
?>
回答by Kai Noack
Quick solution for the nested comment:
嵌套注释的快速解决方案:
Turn the closing */
into a * /
把闭幕*/
变成一个* /
In other words: Just set one whitespace.
换句话说:只需设置一个空格。