PHP Multiple Line Comment inside Multiple Line Com

2019-06-15 00:46发布

问题:

<?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?

回答1:

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

:(



回答2:

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; ?>


回答3:

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
   *

*/


回答4:

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.



回答5:

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");
?>


回答6:

Quick solution for the nested comment:

Turn the closing */ into a * /

In other words: Just set one whitespace.



标签: php comments