Search code examples
phptagscomments

What is the correct way to comment out a PHP variable being echoed using shorthand notation?


I have recently started using the PHP shorthand <?= ?> tags to echo variables etc in my PHP scripts. However I find if I want to then comment out the variable, e.g. <?= //$myVariable; ?> i get a syntax error.

Is it safe to just do this: <?//= $myVariable; ?>

Many Thanks!


Solution

  • The short tag

    <?= ... ?>
    

    is translated into

    <?php echo ...; ?>
    

    So to comment it out, you have to put something into ... that always shows up as empty. Here's the shortest I can come up with:

    <?= false && ... ?>
    

    This works because echo false echoes nothing.

    There's no documentation supporting it, so it might be an old compatibility hack, but the following seem to work:

    <?//= ... ?>
    

    and

    <?/*= ... */?>
    

    Since they're undocumented, I wouldn't depend on them for anything important, but you could use them if you're just temporarily commenting something out while debugging.