0

is there any any other command we can use as an alternative to exit(); in php.

Because it breaks my html code at the end of the page if the condition is not met and when script has to exit.

Or if anyone has any other idea to resolve this issue???

Thanks

Update:

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...

Now the problem is if any of the condition is not met and the script has to exit, the html code below it, which is a whole webpage, does not display...

And please...I am not a computer geek, had a problem so asked it, but why people vote down the question??? don't understand....

sohal07
  • 440
  • 8
  • 21
  • You probably shouldn't be using `exit` if you don't want the page flow to stop immediately. Can we see your code? (relevant parts only) – Jocelyn Aug 25 '12 at 11:58
  • Please help others to help you by providing more detail about your code and your problem: What functionality are you currently using `exit()` for for which you want an alternative? What do you mean by "it breaks my html code"? What condition are you referring to in "if the condition is not met"? – IMSoP Aug 25 '12 at 12:09

2 Answers2

3

You should probably wrap your code into an if statement:

<?php
    if($code == 'ok'){
        echo 'ok';
    } else {
        echo 'not ok';
    }
?>

your script doesn't have to exit(), you can add statements where you want and how you want.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
0

As the name suggests, the PHP exit() statement will cause your PHP script to exit, right there and then, and not do anything else. If you want it to carry on processing the rest of the code, just don't use exit().

Looking at your code, what you seem to be aiming for is displaying errors to the user, and then (I would guess) re-showing the form they filled in incorrectly.

Rather than just echoing the errors as soon as you discover them, why not store them into a variable, which can then be displayed at an appropriate point in the HTML? Even the most basic of scripts can benefit from a bit of basic code structure.

As an example (and I stress this is not the One True Pattern for this kind of thing), you could arrange your file something like this:

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
}
IMSoP
  • 89,526
  • 13
  • 117
  • 169