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

[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:
echo $this->load->view('outer', $data);
But that doesn't give me my benchmark info. The output still contains "{elapsed_time}" and "{memory_usage}".

This code seems to fix that:
$this->load->view('outer', $data);
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?


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

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'


Correction to eggshape's reply: to bail out of a loop, use break, not continue.

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

Theme © iAndrew 2016 - Forum software by © MyBB