如何在 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
  b ".// I want to comment here but it becomes a string instead."  
  "c";
echo $var;

只有连接和注释才能实现:

$var = "a'n" .
//     "b'n" .
       "c";

你也可以这样编写代码。如果你想要多行,你可以在字符串后面使用'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

在编辑器中检查代码和输出。点击这里

你得到了答案,但我想我也会分享我的 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";

当我们使用它时,您可以在样式标签(CSS(中使用/**/:

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

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

$var = "a".
       "b". //Comment
       "c";

如果要维护新行

$var = "a'n"