0

It's a fictitious example. And I want to understand "What really happens when using EXIT in this code":

function my_template_redirect() {
    $page = get_query_var('pagename'); //get some value
    if ( $page == 'products' ): //compare with something
        //show products
        EXIT;
    elseif ( $page == 'product' ): //compare "else"
        //show product
        EXIT;
    endif;
}
add_filter( 'template_redirect', 'my_template_redirect' );

Ok. Step by step execute (my own logical version)

|-code executing
|-filter callback my_template_redirect()
|-if start (or else)
   |-exit
   |-start execute new template

What happen after EXIT call? Control structure IF:ENDIF automatically closes? Or still opened in new code? Is this bad way to organize code?

Thanks in advance!

PS I'm using WP example for better understanding my bad English :)

2
  • exit terminates the script. Nothing can happen "after" calling exit because the script is dead. e.g. RTFM: php.net/exit Commented Nov 7, 2014 at 15:41
  • 1
    You'd probably be better off using return in these instances. Commented Nov 7, 2014 at 15:43

1 Answer 1

1

If you read the PHP docs on exit(), you'll find that exit completely terminates the current script.

However:

Shutdown functions and object destructors will always be executed even if exit is called.

Sign up to request clarification or add additional context in comments.

5 Comments

I know this. What about IF:ENDIF?
As I mentioned, using exit in your example above, will completely terminate the script. It doesn't matter whether it's in if or endif. The function just stops running.
Still opened or will close? It possible leak memory?
No, it's not still open. The script terminates. Terminates. At the end of any PHP process, the process ends just like any other program. Terminates. No memory is left over. Terminates. No memory leak. Terminates. ;-)
:) Thanks! +1! But I'm waiting one day (maybe anyone has another opinion). I like your answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.