0

I am writing a web page, which searches in a table and displays the data if all is well and a message if not. The code looks roughly like this:

<?php session_start(); ?>
<html><head> ..... </head>
    <body>
         <form action="search.php" method="post"> {parameters for searching} </form
         <?php include_once('display_results.php');?>
         <?php if (isset($_SESSION['message'])) {
                   echo "<script type=\"text/javascript\">alert(\"".$_SESSION['message']."\")</script>";
                   unset($_SESSION['message']);
               } ?>
     </body>
</html>

the display_results.php looks like this:

<?php
     //query table get results build output and if there was an error call do_exit($message)

 function do_exit($message) {
    //unset objects close connections and misc finalizations
    $_SESSION['message'] = $message;
    die();
 }

My problem is that when die() is called, it seems that not only does the display_results.php script stops, but also the original php file, so even though the message is set, there is no alert. On the other hand, if I remove the die() statement, and after each do_exit() I execute return this works correctly.

Now this isn't a huge problem, I can just make it so that I call return after each do_exit() but I was wandering if there was a way to exit the script but continue with the execution of the original php file

Thanks in advance for any help you can provide.

Cruces
  • 3,029
  • 1
  • 26
  • 55
  • 1
    Do return instead of die – Islam Zedan May 05 '15 at 08:25
  • 1
    `die — synonym for exit` and `exit — Output a message and terminate the current script` Does what it says on the can, the keyword is __terminate__.... script !== file, script === request – Mark Baker May 05 '15 at 08:27
  • To *include* a php file is basically the same as putting that files content at the spot of the include **statement** (not function, drop the braces!). So you should not think of individual files as individual scripts. It's still one big script, which you're terminating. – Yoshi May 05 '15 at 08:27
  • PHP has program flow statements like "return" and "break" or "continue" are important to know as well for loops. I personally would keep my hands from the "goto" thingy... – chozilla May 05 '15 at 08:47

3 Answers3

2

Why would you want to do that? Just exit a function correctly (with return). It would be possible to do: return do_exit(); and replace die() in your do_exist() function with return; or return false; (if you want to return a value).

die() (or exit()) always ends the script execution, not only the execution of one script-file or a function, that wouldn't be useful.

Florian
  • 2,796
  • 1
  • 15
  • 25
  • yes but return inside do_exit() will just return from the do_exit function, I wish to stop the script and continue with the execution of the previous script; – Cruces May 05 '15 at 08:33
  • You executes only _one_ script :) You can't end the execution of a file, you always have to end the execution of a function, loop structure or script. – Florian May 05 '15 at 08:34
  • oh so it's like when the page is displayed, the display_results script becomes part of my original one because I included it, yeah I should have known that I guess :) but isn't there a way to do something like break 2 which breaks 2 loops up, with return? – Cruces May 05 '15 at 08:38
  • See my comment: http://stackoverflow.com/questions/30047708/phphow-to-continue-execution-after-exiting-from-a-php-script/30047788?noredirect=1#comment48211054_30047852 – Florian May 05 '15 at 08:39
1

why don't you use return false; at the end of your function do_exit()?

  • because return false; would work on the do_exit function, and return the execution to the normal flow, i.e. execute the rest of the display_results.php , I wish to stop that script from continuing and execute the rest of the script that called it – Cruces May 05 '15 at 08:40
  • @Cruces what are you trying to do? If you want to show error messages if some code/query go wrong then why don't you use try-catch? – Hassan Murtaza May 05 '15 at 09:16
0

You don't actually have to call die() at all. Just let the script come to a natural ending. I assume you return results using echo or print statements. If you need performance you can close the session so that other calls can run on it.

  • I can't let the script continue, because as you said I use echo to print out a table with the results (which without data would just show a header and an empty line), I wish to stop the display_result.php at the point of a problem (i.e. no result returned from the query, no connection to the server etc) and continue on with the parent (so that the alert with the error can be shown btw what did you mean by "If you need performance you can close the session so that other calls can run on it" could you explain it please, I am new to php so I didn't quite understand it – Cruces May 05 '15 at 08:35
  • I think your entire code structure doesn't represent what PHP can do. If you want to organize your code, use object, classes and functions. E.g. write your code of display_result.php into an own class/function and run this function only, if there are results returned from your result fetching object/function, which used your database object to fetch the results. – Florian May 05 '15 at 08:38
  • Ahh now I understand your problem. You have to return an error message as part of the return value and parse it on the client. It is common to return an error code with a message e.g. echo '{"E":{"eCode":99,"eText":"A problem"}}'. You then test the data value returned in javascript in the client. You have to write code in the client to handle this. – Barry Keepence May 05 '15 at 15:40