如何在脚本中间结束PHP脚本,但显示页面的其余部分
I have my long PHP script with some JavaScript at the end. In the middle of the script if an 'if' statement is true then is their a way to stop the rest of the PHP script and output the rest of the HTML part of the page. Here's a little snippet:
... more code ...
case 2:
$e=$_POST['email'];
$n=$_POST['name'];
mysql_connect($dbloc,$dbuser,$dbpass);
mysql_select_db($dbname)or die(mysql_error());
$query="SELECT * FROM users WHERE email='$e'";
$re=mysql_query($query);
$numm=mysql_numrows($re);
if($numm>=1){
$js="alert('Sorry but your email is currently in use. Please try again with a different email address.');";
$url="./";
exit();
}
else{
$_SESSION['rEmail']=$e;
$_SESSION['rName']=$n;
}
break;
... more code...
<html goes here />
I originally thought exit()
would end just the PHP portion but still give back the rest of the page but that does not seem to be the case.
exit()
is performing what it was maid to do. It is exiting the your PHP code right from the point where it is called.
Your concept that it'll print the remaining of HTML is wrong. The whole of PHP code is taken as a PHP script, with all your logic/PHP functions tagged inside <?php
and ?>
and the text outside is just like documentation, and is outputted as it is.
With the exit inside the if
statement, the code/text after it isn't processed at all. I'd suggest that you use a break
statement instead.