andy5000 Posted July 23, 2010 Share Posted July 23, 2010 I don't know if I have been awake too long and im missing something stupidly obvious, but I cannot work out why this script isn't behaving as expected. I have simplified it down as much as I can in my code yet it is still giving me problems: if (1 == 1) { header("location:1.php"); } else { header("location:2.php"); } // other code down here When running this, I would expect to be relocated to 1.php, however, my "other code" is running instead. The weird part is that if I do this: if (1 == 1) { header("location:1.php"); } else { header("location:2.php"); } exit(); // other code down here I do get relocated. Im sure its really simple, but I am just lost. I have been hopping in between PHP and Coldfusion all day so I might just be getting some basic syntax wrong.. I don't know. Any help would be very much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/208704-neither-if-or-else-code-running/ Share on other sites More sharing options...
DavidAM Posted July 23, 2010 Share Posted July 23, 2010 The header() function does NOT end the script. It will continue to run. If you want to redirect immediately, you need to exit the script immediately. if (1 == 1) { header("location:1.php"); exit(); } else { header("location:2.php"); exit(); } // other code down here Quote Link to comment https://forums.phpfreaks.com/topic/208704-neither-if-or-else-code-running/#findComment-1090335 Share on other sites More sharing options...
andy5000 Posted July 23, 2010 Author Share Posted July 23, 2010 Thankyou David, I've been teaching myself PHP for a little while now, but didn't even realise I was supposed to be doing it like this. I think I must have been quite lucky not to have come across this problem before considering that how I always do my relocates! Thanks again - you learn something every day Quote Link to comment https://forums.phpfreaks.com/topic/208704-neither-if-or-else-code-running/#findComment-1090344 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.