PHP Multiple Line Comment inside Multiple Line Com

2019-06-15 01:03发布

<?php
/*
    /* this is a comment */
*/
?>

PHP returns "syntax error"...
Is this just a completely wrong way to use multiple line comment?

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?

标签: php comments
6条回答
干净又极端
2楼-- · 2019-06-15 01:08

Quick solution for the nested comment:

Turn the closing */ into a * /

In other words: Just set one whitespace.

查看更多
祖国的老花朵
3楼-- · 2019-06-15 01:09

From the PHP manual:

'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 */
 */
?>

:(

查看更多
孤傲高冷的网名
4楼-- · 2019-06-15 01:12

By design PHP syntax won't allow to do that.

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
   *

*/
查看更多
Deceive 欺骗
5楼-- · 2019-06-15 01:27

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; ?>
查看更多
啃猪蹄的小仙女
6楼-- · 2019-06-15 01:30

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.

If your IDE doesn't have that feature, then I think you're out of luck.

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:

// $a = 1; /* sets a = 1 */
// $b = 2;
// /*
//     blah blah
// */

The // comments win, which mean you just did what you're trying to accomplish.

查看更多
女痞
7楼-- · 2019-06-15 01:32

for smart move just add and save you whole desire comment code section in "yourCodeBlock.php"then

<?php 
/*
include("yourCodeBlock.php");
*/
?>

or simple single line comment

<?php 
//include("yourCodeBlock.php");
?>
查看更多
登录 后发表回答