Jump to content

Neither "if" or "else" code running


andy5000

Recommended Posts

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.

 

:shrug:

 

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!

Link to comment
https://forums.phpfreaks.com/topic/208704-neither-if-or-else-code-running/
Share on other sites

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

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.