Why does the order of comands matter in this case?

I find that

// send email
$success = mail($EmailTo, $Subject, $Body, “From: <$EmailFrom>”);

Must precede the redirection section regardless of which of two redirection methods are used. One method is this:

// print redirect
if ($success){
print “<meta http-equiv=\“refresh\” content=\“0;URL=/ok.htm\”>”;
}
else{
print “<meta http-equiv=\“refresh\” content=\“0;URL=/error.htm\”>”;
}

The other is this:

// Header redirect:
if ($success) {
header(‘Location: http://www.globalfreeenterprise.com/ok.htm’);
} else{
header(‘Location: http://www.globalfreeenterprise.com/error.htm’);
}

It seems that if

$success = mail($EmailTo, $Subject, $Body, “From: <$EmailFrom>”);

follows either of the above methods of redirection, the file does not work properly. Only the error.htm file is invoked but never the ok.htm file. Does anybody know why? Why does the order of commands matter in this case?

Thank you for your help

Jim Adrian
<snip/>

As I understand the replies,

$success = mail($EmailTo, $Subject, $Body, “From: <$EmailFrom>”);

does two things: It gives a value to $success, and it sends an email message.

I was focused on the sending of the email message. It seemed that something must have been set to allow that message to be sent. It seemed that whatever permits the message to be sent could permit it to be sent at any time. I missed the fact that the redirection section does it job on the basis of the value in $success. Please correct me if I’m wrong, but apparently, some mechanism sends the message independent of setting the value of $success.

Thank you for your help.

Jim Adrian
<snip/>

You all have done a great job of clarifying these issues for me.

Thank you for your help.

Jim Adrian
<snip/>

Do not mistake this for “it returns a boolean value which indicates whether the email was correctly received by the recepient.”.
PHP only returns whether it was able to send the e-mail, it’s clueless as to whether the e-mail was actually delivered.

Consider this real world equivalent.

  1. if goods paid for
    then take goods home
    else leave without goods
  2. search store for goods to purchase and if something suitable found then pay for it

If you follow that order of doing your shopping then you will always take the leave without goods for because you haven’t yet paid for the goods that you haven’t yet found.

You must perform step 2 before step 1 in order for step one to ever take the take goods home path and the same applies in programming code.

With your statements reversed your code effectively says

  1. if the email sent properly
    then go here
    else go there
  2. try to send the email and record whether it sent properly or not

Exactly. The real “work” of the statement is sending the email which is performed by the mail() function. If that was it, this would technically be a routine with “void” output. However, mail() does return a value, so it’s techinically a function; it returns a boolean value which indicates whether the email was correctly sent. That’s why you can then use $success (but only after you’ve tried to send the email, because otherwise the mail() function hasn’t returned a value for it yet) in an if statement.

Because if it goes after when you get to your condition $sucess has never been set and therefore is false and will always redirect to your error page.

The “order of commands” always matters and it not just limited to your case.