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

What's the best way to comment out this big block? Besides removing it temporarily from the file?

1
  • Why would you want to do that? Commented Jan 31, 2022 at 14:10

6 Answers 6

14

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

:(

Sign up to request clarification or add additional context in comments.

5 Comments

so are you saying that the only thing I could do is to either use bunch of // inside a multiple line comment or remove the block temporarily from the file?
@dudeMatt - Yes, unfortunately - or use an IDE which does 'mass replacement' well. Or create your own script.
It's unreliable because it is feasible to have /* or */ within regular expressions, anyway.
To add to the answer, your FIRST comment block ends when the second comment block ends. So the first comment block become /* echo ... */, and then the last */ is readen as code.
From my understanding comments should be used to detail syntax but only to describe what the block, or statement is doing. Ideally there shouldn't be any regex inside comments.
9

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

2 Comments

I use a similar approach with function ignore(){ ... }
also works... ultimately I started letting my ide do the work... in vim you can easily set your motion to gc and let it add slashes all over the place...
2

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
   *

*/

Comments

1

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 Comments

IDE? Editor? what kind of editor would tell PHP to comment code MY way? NO! it's a PHP related problem...nothing to do with editors...
Dude, relax. All I'm saying is that if your IDE lets you do a one-click comment using single-line comments, then you can comment multiple lines with one click, and it won't matter if there are multi-line comments in that batch. See my edit.
Ahhh, brings back memories of eclipse... 'Ctrl->/' and all is commented
@dudeMatt - if it makes you feel any better, the same problem exists in JavaScript, "never not use // in JavaScript because teh lolzboat cud sink" (Oscar Wilde, 1748).
@Joe, alright, my bad. yes, I'm using EditPlus and it does have that feature, but after all it looks bad to me with so many //.
|
1

Quick solution for the nested comment:

Turn the closing */ into a * /

In other words: Just set one whitespace.

Comments

0

For a smart move, just add and save your whole desired comment code section in "yourCodeBlock.php" and then:

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

or a simple single line comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.