Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a web using PHP. When some of the given condition is not true, I use die() function to terminate the rest of the PHP code execution. But with the termination of the PHP code, the HTML code is also terminated. I want such a function that only terminate the PHP execution and does not effect the HTML.
Posted

You could use exit. Have a look at this link:
http://php.net/manual/en/function.exit.php[^]

Good luck!
 
Share this answer
 
Comments
rashidfarooq 15-Apr-11 9:02am    
brother exit also terminates the html code.
E.F. Nijboer 15-Apr-11 9:50am    
Example 3 looked like it did what you looked for but just only for destructors. In the comments below it however there are some ideas about it though. Maybe a flush()/ob_flush() might work before calling exit or die but terminating the request half way would break the html. Some tags might not get closed, like body and html. Would be best to create a proper flow that fully completes. Depending on what kind of project you are working on you will probably come across this later on and it will give you major problems.
Use try catch then throw the exception as html

XML
<html>
    <head><title></title></head>
    <body>
<?php
        echo "Let me see If I can connect to the database.....";
        try
        {
            $myObj= new MyDbWrapper();
            $myObj->connectAndSelectDb('not existing db');
            echo "Yes I am the winner";
        }catch (Exception $e)
        {
            echo $e;
        }
?>
        <!-- your htmls here-->
        <br/>
        <input type="text" name="text1" /><br/>
        <input type="text" name="text2" /><br/>
        <input type="text" name="text2" /><br/>
        <div>Scrape whatever you want</div>
<?php
        class MyDbWrapper
        {
            function connectAndSelectDb($dbName)
            {
                mysql_connect('localhost','root','');
                if(!mysql_select_db($dbName))
                {
                    throw new Exception(mysql_error(). "... Oops an error");
                }
            }
        }

?>
</body>
</html>
 
Share this answer
 
v2
Comments
rashidfarooq 16-Apr-11 8:40am    
can you plz explain your suggestion with any example.
Albin Abel 16-Apr-11 10:53am    
looks like this question is solved. the code, right here

<html>
<head><title></title></head>
<body>
connectAndSelectDb('not existing db');
echo "Yes I am the winner";
}catch (Exception $e)
{
echo $e;
}
?>
<!-- your htmls here-->
<br/>
<input type="text" name="text1" /><br/>
<input type="text" name="text2" /><br/>
<input type="text" name="text2" /><br/>
<div>Scrape whatever you want</div>


</body>
</html>
Albin Abel 16-Apr-11 10:54am    
it trim out the code. Ill add up in my answer.
rashidfarooq 16-Apr-11 14:42pm    
Thanks a lot for answering me so briefly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900