php显示错误消息并清除内容

php显示错误消息并清除内容,php,Php,我希望代码显示错误消息,同时删除页面内容 例如 示例删除所有内容但保留错误(“您无法访问此页面”) 添加exit()在if语句中。以您的例子: <? echo 'welcome'; if (login==0) { error("you can't access to this page"); exit(); } content, content, content, content, content, content, content, content, content, c

我希望代码显示错误消息,同时删除页面内容

例如

示例删除所有内容但保留错误(“您无法访问此页面”)

添加
exit()在if语句中。以您的例子:

<?
echo 'welcome';
if (login==0)
{
    error("you can't access to this page");
    exit();
}
content, content, content, content, content, content, content, content, content, content
ect.......
?>

您也可以使用Matt提供的功能,这里还有另一种选择:

<?
echo 'welcome';
if(login==0) {
  error("you can't access to this page");
} else {
?>
content, content, content, content, content, content, content, content, content, content
<?php
}
?>

使用输出缓冲:Matt Harvey的答案是最简单的解决方案。您可能还希望在if(login==“0”)条件检查之后移动echo“welcome”,这样您就不会看到欢迎消息,然后会看到“You not access to this page”消息我希望清理所有输出并只保留“You not access to this page”是的,只需移动
echo“welcome”
到关闭if语句的
}
之后。否则,在输出错误之前,需要使用Javascript清除页面内容。此外,您还可以更改
错误(“…”)
只需
回显
错误消息。
<?
echo 'welcome';
if (login==0)
{
    error("you can't access to this page");
    exit();
}
content, content, content, content, content, content, content, content, content, content
ect.......
?>
<?
echo 'welcome';
if(login==0) {
  error("you can't access to this page");
} else {
?>
content, content, content, content, content, content, content, content, content, content
<?php
}
?>