有没有其他命令可以作为exit()的替代命令;在php中。
因为如果不满足条件并且脚本必须退出,它会在页面末尾中断我的html代码。
或者,如果有人有其他想法来解决这个问题???
感谢
更新:
html code...
<?php
if username is not in correct format
echo "Please check your username";
exit();
if Username and Password didn't match
echo "Wrong Username or Password.";
exit();
if some other condition not met
echo "Condition not met";
exit();
?>
html code continues...
现在的问题是,如果任何一个条件都不满足,脚本必须退出,那么它下面的html代码,即整个网页,将不会显示。。。
请。。。我不是一个电脑怪胎,有问题就这么问,但为什么人们会投票否决这个问题???不明白。。。。
您可能应该将代码包装成if
语句:
<?php
if($code == 'ok'){
echo 'ok';
} else {
echo 'not ok';
}
?>
您的脚本不需要exit()
,您可以在需要的位置和方式添加语句。
顾名思义,PHP exit()
语句将导致您的PHP脚本立即退出,而不执行任何其他操作。如果您希望它继续处理其余的代码,请不要使用exit()
。
查看您的代码,您的目标似乎是向用户显示错误,然后(我猜)重新显示他们错误填写的表单。
与其在发现错误后立即回显,为什么不将它们存储到一个变量中,然后在HTML中的适当位置显示呢?即使是最基本的脚本也可以从一些基本的代码结构中受益。
举个例子(我强调这不是这类事情的唯一真实模式),你可以这样安排你的文件:
if ( /* form has been submitted */ )
{
$errors = validate_form();
if ( count($errors) > 0 )
{
display_form($errors);
}
else
{
display_success_message();
}
}
else
{
display_form();
}
function validate_form()
{
$errors = array();
// Series of if conditions, each adding a message to $errors if appropriate
return $errors;
}
function display_form($errors=array())
{
// HTML <ul> list displaying the contents of $errors, if any
// HTML for form
}
function display_success_message()
{
// HTML thanking user for a successful form submission
}