如何在 PHP 中的字符串内发表评论

How to comment inside a string in PHP

$var = "a
  b // I want to comment here but it becomes a string instead
  c"

我想在 PHP 中的多行字符串中间插入注释,但我不能。我试过 /**///#

有人知道怎么做吗?

只有串联和注释才有可能:

$var = "a\n" .
//     "b\n" .
       "c";
$var = "a
  b ".// I want to comment here but it becomes a string instead."  
  "c";

echo $var;

你也可以这样写你的代码。如果你想要多行,你可以在字符串之后使用 \n

$string = 'First';
$string .= 'Comment section ';//this where you can comment
$string .= 'Last';
<?php
$var = "a\n" .
"b\n" . //this where you can comment
"c";
echo $var;
?>

输出

a
b
c

在编辑器中检查代码和输出。 Click Here

你得到了答案,但我想我也会分享我的 2 美分: 您可以使用 HTML 评论标签:

<!--This is a comment. Comments are not displayed in the browser-->

要注意的是,用户如果查看源代码就可以查看此评论!

以你举的例子:

$var = "a
  b <!--I want to comment here but it becomes a string instead-->
  c";

当我们这样做时,您可以在 Style 标签中使用 /**/(CSS):

.Class {
  width: 100%; /*The width*/
}

或者正如有人已经建议的那样,只需关闭字符串并在 PHP:

中进行评论
$var = "a".
       "b". //Comment
       "c";

如果您想维护新行

$var = "a\n"