View Full Version : Exiting a function, not the subsequent HTML

05-20-2004, 07:34 PM

I have an HTML file with a section of PHP code in it. The PHP function contains an if / else statement. If the first 'if' criteria is met then i want the php code to end, but load the rest of the HTML. The problem I am having at the moment is when using the exit() command, the remaining HTML does not load.

Is there any way around this? Any help would be greatly appreciated.


05-20-2004, 07:44 PM
just leave the if brackets empty:

if ($true) {
// do nothing, HTML loads
} else {
// do your php stuff
you could do this instead:
if (!$true) {
// do your php stuff

05-20-2004, 08:31 PM
Hi, thanks for replying so quickly.

The problem i am facing is that in the second 'if' section (1) the code checks to see whether a persons birthdate matches the date stored in the database (the first if statement checks to see whether the header contains the word continue). If this criteria is matched then this section echo's a form to the user to check the time of birth.

If the time of birth is before a set time then the user is sent to a different php page because a different calculation must be done.

If it is after the set time then the word 'continue' is written into the header and the 'if' section (1) is bypassed and the else statement continues to gather the information that is required from the database.

The error does not occur if the persons birthdate is not the same as one in the database because it will bypass the first if statement.

The problem occurs when it does the first check to see first of all if the word continue is present and then if the date matches. If the date is the same it displays the new form and runs the exit() command, but this stops the rest of the html from being displayed. If i take out this exit command it continues on and displays both the form and the 'taster'.

I have included the code below to clarify the explanation.

$db = @mysql_connect("localhost","test","test");
if (!$db)
echo ("<P>unable to connect to Database</P>");

//select what database to use

if (! mysql_select_db("test"))
echo ("<P> Unable to locate data base</P>");

//end db connection

// get the day, month and year from the header
if (isset($_GET['day']))
$day = $_GET['day'];
$month = $_GET['month'];
$year = $_GET['year'];
$fulldate = (''.$day.'-'.$month.'-'.$year.'');
$testdate = (''.$year.'-'.$month.'-'.$day.'');
echo ('<strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">'.$fulldate.'</font>');

$result = mysql_query("SELECT *
FROM `signs`
WHERE `Date` <= '$year-$month-$day'
LIMIT 0 , 1

if (!$result)
echo ("error with the query");

// get the sign, date and time from the database

$data = mysql_fetch_array($result);

$sign = $data['Sign'];
$date = $data['Date'];
$time = $data['Time'];

// if statement to check if birthdate is same as change date
// random number code between 0 and 3 to select different 'snippets' each time

$random = (rand()%3);
echo $random;
// random number code end

// query to select the taster from the database.
$tasterresult = mysql_query("SELECT *

FROM `taster`

WHERE `taster_typ` LIKE '%$sign%'

AND `taster_num` LIKE '$random'


if (!isset($_GET['continue']))
if ($testdate == $date)

echo ('<br><br><strong><font size="3" face="Verdana, Arial, Helvetica, sans-serif">Were you born before or after '.$time.' on '.$day.'-'.$month.'-'.$year.'?<br></font>
<form name="timecheck" method="GET" action="'.$_SERVER['PHP_SELF'].'">
<input type="hidden" name="day" value="'.$day.'">
<input type="hidden" name="month" value="'.$month.'">
<input type="hidden" name="year" value="'.$year.'">
<input type="hidden" name="continue" value="continue">
<input type="submit" name="Submit" value="After this time">
<form name="timecheck2" method="GET" action="tasterextra.php">
<input type="hidden" name="day" value="'.$day.'">
<input type="hidden" name="month" value="'.$month.'">
<input type="hidden" name="year" value="'.$year.'">
<input type="hidden" name="cdate" value="'.$date.'">
<input type="submit" name="Submit" value="Before this time">



//if (!$tasterresult)
//echo ("error with the query");
$tasterdata = mysql_fetch_array($tasterresult);

$taster = $tasterdata['taster_text'];

echo ('<br><br><strong><font size="3" face="Verdana, Arial, Helvetica, sans-serif"><em>


header ('Location:taster.php');
Thanks once again for any help.


05-20-2004, 09:58 PM
exit() is an alias of die().

When in a function and you want to stop the function from executing anymore PHP code, use return 0;

Even if your function isn't supposed to return anything, use return 0; It will exit the function and continue with whatever code is outside of the function, and then the HTML code.

05-20-2004, 10:38 PM
Could you not use


05-21-2004, 01:02 PM
instead of
echo $HTML;
$str = $HTML; (and use $str .= $moreHTML; if required further on)

remove the exit call;
then assuming your code gets past the location header ..

echo $str;

05-21-2004, 04:27 PM

Thanks for all the help, i used the $str = html method and the problem is now sorted.

Paul :thumbsup: