使用php die / exit阻止html执行?

时间:2013-10-05 13:47:46

标签: php

似乎die()exit()只阻止运行下面的php而不是html。

如何使用php if else语句来显示html?我不喜欢echo,因为我想阻止用户看到我的整个代码(从<body&gt;到</body>

3 个答案:

答案 0 :(得分:1)

die()exit() 停止所有脚本,甚至html输出,当文件由php执行时

<?php
header('Content-Type: text/plain; charset=utf-8');
?>

<html>

<?php exit(); ?>

</html>

输出:

<html>

另一种方法是使用return来停止通用脚本:

<?php
header('Content-Type: text/plain; charset=utf-8');
?>

<html>

<?php return; ?>

</html>

但它只停止当前的脚本。


是的,您可以使用条件语句控制输出,如下所示:

<?php
header('Content-Type: text/plain; charset=utf-8');

$showbody = false;
?>

<html>

<?php if($showbody): ?>
    <body>
        Here is the body!
    </body>
<?php endif; ?>

</html>

输出:

<html>


</html>

答案 1 :(得分:0)

<?php
if(false){
   die();
}else{ php?>
  <body>

  </body>
<?php }?>

这很难看,但我认为这就是你要找的东西

答案 2 :(得分:0)

您将需要使用PHP的Output buffering函数。

基本上如果你没有启用输出缓冲,只要有回音,打印或html,它会立即开始发送到客户端。启用输出缓冲后,它将全部存储,您可以控制何时或甚至是否已发送。