0

How would I go about commenting all of this code when it has breaks in the PHP sections?

If I wrap /* */ around it, it doesn't work.

Obviously I can make it work by not being lazy, but if I want to be lazy... How might you comment this whole block?

if($fields){
    ?>
        <ul>
            <?php
                foreach($fields as $field){
                    ?>
                        <li>
                            <?php
                                /* if($field['label']){
                                    echo $field['label'];
                                } */
                                print_ext($field);
                            ?>
                        </li>
                    <?php
                }
            ?>
        </ul>
    <?php
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • Do you mean comment out the HTML so that it doesn't display? – BenM Aug 07 '12 at 15:59
  • 1
    I wonder if you could use HEREDOC to assign it all to some unused variable. – WWW Aug 07 '12 at 16:00
  • He cant just comment the html because of the ?> it would still mean commenting every line as he switches back and forth between HTML and PHP – ian.shaun.thomas Aug 07 '12 at 16:01
  • 1
    Comment out the HTML right before and after it (``). This will hide the HTML and anything generated by the PHP. – j08691 Aug 07 '12 at 16:01
  • But it will also mean running the php.. which is no good.. But thanks for the try :) – Jimmyt1988 Aug 07 '12 at 16:04
  • @Crontab : see my answer, which uses nowdoc – greg0ire Aug 07 '12 at 16:11
  • Re *"it doesn't work."*: That could be more informative. Does it stop with a syntax error (a blank web page (or with very little content) may or may not be the result)? Are entries turning up in the web server log file (and crippled web page content to some degree)? Or something else? Please respond by [editing (changing) your question](https://stackoverflow.com/posts/11849745/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the question should appear as if it was written today). – Peter Mortensen Sep 29 '21 at 00:15

4 Answers4

4

You can't really but you can turn it off pretty easily.

if($fields && false){
    ?>
        <ul>
            <?php
                foreach($fields as $field){
                    ?>
                        <li>
                            <?php 
                                /*if($field['label']){
                                    echo $field['label'];
                                }*/
                                print_ext($field); 
                            ?>
                        </li>
                    <?php                                                        
                }
            ?>
        </ul>
    <?php
} 
Rob Forrest
  • 7,329
  • 7
  • 52
  • 69
1

The following solution should work. You might have issues if you are wrapping the comments that exist already around if($field['label']) so I have deleted them as shown below.

<?php
/*
if($fields){
    ?>
        <ul>
            <?php
                foreach($fields as $field){
                    ?>
                        <li>
                            <?php 
                                if($field['label']){
                                    echo $field['label'];
                                }
                                print_ext($field); 
                            ?>
                        </li>
                    <?php                                                        
                }
            ?>
        </ul>
    <?php
} 
*/
?>

For more information look at this answer.

Community
  • 1
  • 1
user1474090
  • 675
  • 6
  • 5
  • Can you elaborate on *why* (or if the poster has left the building, somebody else - we no longer have this information)? Nested comments are not allowed? Link to authoritative documentation that states it? Please respond by [editing (changing) your answer](https://stackoverflow.com/a/11849856/63550), not here in comments (***without*** "Edit:", "Update:", or similar - the question/answer should appear as if it was written today). – Peter Mortensen Oct 18 '21 at 15:16
0

Not really commenting, but you can disable this block like this (almost no matter its content):

<?php $bar = <<<'EOD'
if($fields && false){
  ?>
    <ul>
    <?php
    foreach($fields as $field){
      ?>
        <li>
        <?php
        /*if($field['label']){
          echo $field['label'];
          }*/
        print_ext($field);
      ?>
        </li>
        <?php
    }
  ?>
    </ul>
    <?php
}
EOD;
greg0ire
  • 22,714
  • 16
  • 72
  • 101
0

Put your HTML within the PHP open/close tags and then the /* */ will work fine.

JosephM
  • 26
  • 2