Welcome Guest, Not a member yet? Register   Sign In
Exit early and still output benchmark info
#1

[eluser]The Fox[/eluser]
Hi all,

I sometimes want to end script execution early if there's an error so I don't have too many levels of if statements. The example I've been working on is a page that displays the details of one record from the database. The query must return exactly one result, otherwise I want to give an error message and end the script right there.

The most obvious code to do this is:
Code:
echo $this->load->view('outer', $data);
exit;
But that doesn't give me my benchmark info. The output still contains "{elapsed_time}" and "{memory_usage}".

This code seems to fix that:
Code:
$this->load->view('outer', $data);
$this->output->_display();
exit;
However, calling CI's private functions myself seems like a hack. Is there a better way to do this? Am I overlooking something that will break in the future?

Thanks,
Matt

PS I've a few months in to coding with CI, and enjoying PHP again. Thanks Derek and everyone!
#2

[eluser]eggshape[/eluser]
Hi Fox - If you want to exit from a function but not exit the script, use 'return' (you don't need to return a specific value); If you want to exit out of a loop, use 'continue'

http://us2.php.net/manual/en/control-str...ntinue.php
#3

[eluser]BoltClock[/eluser]
Correction to eggshape's reply: to bail out of a loop, use break, not continue.
#4

[eluser]The Fox[/eluser]
Thanks egg- I'm so used to procedural PHP I forgot the controller is a function and I can use
Code:
return;
to jump out. :red: That's so much easier.




Theme © iAndrew 2016 - Forum software by © MyBB