[php] HTML과 PHP를 함께 주석 처리

이 코드가 있습니다.

    <tr>
      <td><?php echo $entry_keyword; ?></td>
      <td><input type="text" name="keyword" value="<?php echo $keyword; ?>" /></td>
    </tr>
    <tr>
      <td><?php echo $entry_sort_order; ?></td>
      <td><input name="sort_order" value="<?php echo $sort_order; ?>" size="1" /></td>
    </tr>

한 번에 둘 다 댓글을 달고 싶습니다 …하지만 시도 할 때

    <!-- <tr>
      <td><?php echo $entry_keyword; ?></td>
      <td><input type="text" name="keyword" value="<?php echo $keyword; ?>" /></td>
    </tr>
    <tr>
      <td><?php echo $entry_sort_order; ?></td>
      <td><input name="sort_order" value="<?php echo $sort_order; ?>" size="1" /></td>
    </tr> -->

페이지가 실패합니다. PHP 코드가 주석 처리되지 않은 것 같습니다. 이렇게 할 수있는 방법이 있습니까?



답변

HTML 주석을 사용하는 대신 (PHP 코드에 영향을주지 않고 계속 실행 됨) PHP 주석을 사용해야합니다.

<?php /*
<tr>
      <td><?php echo $entry_keyword; ?></td>
      <td><input type="text" name="keyword" value="<?php echo $keyword; ?>" /></td>
    </tr>
    <tr>
      <td><?php echo $entry_sort_order; ?></td>
      <td><input name="sort_order" value="<?php echo $sort_order; ?>" size="1" /></td>
    </tr>
*/ ?>

그러면 HTML 내부의 PHP 코드가 실행되지 않습니다. 그리고 아무것도 (HTML이 아니라 PHP가 아니라 실행되지 않은 결과가 아닙니다) .

참고 : C 스타일 주석을 중첩 할 수 없습니다. 즉, 주석이 처음 */발견 될 때 끝납니다 .


답변

나는 Pascal의 솔루션이 갈 길이라는 데 동의하지만 주석을 제거하는 추가 작업을 추가한다고 말하는 사람들을 위해 다음 주석 스타일 트릭을 사용하여 생활을 단순화 할 수 있습니다.

<?php /* ?>
<tr>
      <td><?php echo $entry_keyword; ?></td>
      <td><input type="text" name="keyword" value="<?php echo $keyword; ?>" /></td>
    </tr>
    <tr>
      <td><?php echo $entry_sort_order; ?></td>
      <td><input name="sort_order" value="<?php echo $sort_order; ?>" size="1" /></td>
    </tr>
<?php // */ ?>

코드 블록이 주석 처리되는 것을 중지하려면 여는 주석을 다음과 같이 변경하십시오.

<?php //* ?>


답변

중첩 된 HTML + PHP 코드를 많이 주석 처리해야하는 경우 다음 솔루션이 매우 효과적이라는 것을 알았습니다.

모든 내용을 다음과 같이 포장하십시오.

<?php
    if(false){
?>

Here goes your PHP + HTML code

<?php
    }
?>


답변

그만큼 <!-- --> HTML 주석을위한 것이며 PHP는 어쨌든 여전히 실행될 것입니다.

따라서 내가 할 가장 좋은 일은 PHP를 주석 처리하는 것입니다.


답변

PHP 주석으로 만이 작업을 수행 할 수 있습니다.

 <!-- <tr>
      <td><?php //echo $entry_keyword; ?></td>
      <td><input type="text" name="keyword" value="<?php //echo $keyword; ?>" /></td>
    </tr>
    <tr>
      <td><?php //echo $entry_sort_order; ?></td>
      <td><input name="sort_order" value="<?php //echo $sort_order; ?>" size="1" /></td>
    </tr> -->

PHP와 HTML이 작동하는 방식은 다음과 같이하지 않으면 한 번에 주석을 달 수 없습니다.

<?php

/*

echo <<<ENDHTML
 <tr>
          <td>{$entry_keyword}</td>
          <td><input type="text" name="keyword" value="{echo $keyword}" /></td>
        </tr>
        <tr>
          <td>{$entry_sort_order}</td>
          <td><input name="sort_order" value="{$sort_order}" size="1" /></td>
        </tr>
ENDHTML;

*/
?>


답변

PHP 파서는 전체 코드를 검색하므로 <?php(또는 <?short_open_tag = On 인 경우) HTML 주석 태그는 PHP 파서 동작에 영향을주지 않으며 PHP 코드를 구문 분석하지 않으려면 PHP 주석 지시문 ( /* */또는 //) 을 사용해야합니다. .


답변

이것을 주석으로 사용할 수도 있습니다.

<?php
    /* get_sidebar(); */

?>