问题
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!
回答1:
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.
回答2:
So on the question of why <?/*=...*/?>
and <?//=...?>
work is because there's a PHP feature called short_open_tag, that lets you skip putting php
after <?
and just go with something like <? echo ...; ?>
. It can be disabled in the INI file, and before v5.4 the short hand wouldn't work unless it was enabled. So as long as you're in control of your INI file you should be OK.
BUT, I just checked our 5.6.31 system (it's old yes), but when the short_open_tag is set to false the <? ... ?>
get emitted directly to the client as if it were HTML text. So, it may be that you just aren't seeing the text because the browser isn't rendering it.
来源:https://stackoverflow.com/questions/24299985/what-is-the-correct-way-to-comment-out-a-php-variable-being-echoed-using-shortha