2

As you know in a php script when you call Return or Die it prevents the rest of HTML codes from rendering.

In an occasion when I just want to stop the php script but not whole the page what would I do?

Ex:

<?php
if(!isset($_POST['txt_username']))
{
    echo "Please enter your username";
    return;
}
?>
<i want="this html">to be rendered</i>

I want my HTML codes to be rendered afterward. Thanks for reading.

Kaii
  • 20,122
  • 3
  • 38
  • 60
Ali.Rashidi
  • 1,284
  • 4
  • 22
  • 51

3 Answers3

1

Your question is not clear, but you need to stop the PHP code to execute, but the HTML to render? You might need output buffer or functions.

E.g.:

<form action="" method="post" >
    <input type="text" name="txt_username" />
    <input type="submit" />
</form>

<?php
function doSmth(&$password) {
    if(!isset($_POST['txt_username']))
    {
        echo "Please enter your username";
        return false;
    }
    $password .= "333";
    echo "You password has been changed to $password";
}

$password = 128;
doSmth($password);
?>
<body>
    <p> <b> Your password is <?= $password; ?> </b></p>
</body>

Examples:

  • Text field is set:
  • Output:

    You password has been changed to 128333

    Your password is 128333


  • Text field is not set:
  • Output:

    Please enter your username

    Your password is 128

Royal Bg
  • 6,988
  • 1
  • 18
  • 24
  • When php interpreter reaches the 'return' or 'die' statement it stops everything. how to avoid that? – Ali.Rashidi Apr 14 '14 at 12:30
  • @AliRashidi when the PHP interpreter reaches `return` in a function call, it will stop everything in the scope of the function, but not the global/outer scope. My example will render HTML even `$_POST['txt_username']` is not set – Royal Bg Apr 14 '14 at 12:31
  • In the mentioned code above when 'txt_username' in undefined you will end up with a blank page. try it – Ali.Rashidi Apr 14 '14 at 12:32
  • @AliRashidi see my new example – Royal Bg Apr 14 '14 at 12:41
  • @AliRashidi the `

    Your password...

    ` will always render, but the logic inside the function will not be done, if the `txt_username` is not set and the proper error message will be displayed E.g.: the password will not change from `128` to `128333` because there is a return
    – Royal Bg Apr 14 '14 at 12:41
0

Use the conditional statement to include more PHP is needed. The HTML can then be rendered regardless of the PHP's if/else statement. Example:

<?php
if( !isset($_POST['txt_username']) )
{
 echo "Please enter your username";
}
else
{
    //Some dditional PHP functions
}
?>
<i want="this html">to be rendered</i>
Nadav
  • 1,055
  • 1
  • 10
  • 23
-1
<?php
$stateOfRender = false;
if(!(isset($_SESSION['sessionid']))) {
    echo "You have not logged yet. Now you are redirecting to Login Page";
    header('Refresh: 5; URL = login.php');
} else {
    $stateOfRender = true;
}

// In everywhere you can use this variable to control your statement
// for example:
if($stateOfRender) {
    // Open DB connection and fetch user data and settle it to page.
}
?>

In my php code i use this method to handle process. Generally i use it in db connection. If dbconnection is success i render page with control this variable. I hope to it helps you.