<?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?
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 */
*/
?>
:(
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 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
*
*/
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.
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");
?>
Quick solution for the nested comment:
Turn the closing */
into a * /
In other words: Just set one whitespace.