PHP code is executed top down, one line at a time.
as soon as it gets to the header function to redirect to another page, it stops processing the current page and loads the next one.
Any time you have multiple server behaviors on a page, only the last one line should set the redirect so they all can execute.