Skip to main content
Drymetal
Inspiring
January 28, 2012
Answered

IF elseif else usage

  • January 28, 2012
  • 1 reply
  • 2074 views

So, now I've been trying to learn If, elseif and else statements properly.  And I'm doing something wrong. 

A guy told me his website would email specific people based on what zipcode they put in the contact form on his site.  I was wondering how he did it, so I tried to recreate something similar.

Here is what I've done:

1. I created a file called zipcodes.php

2. In this file it has set every zipcode to a variable.  For instance:

<?php

$Nashville=("37010" or "37027" or "37040" or "37055" or "37152");

?>

3.  I created a form with a field called 'zip'.

4. I created a php file called phpmail.php to send this form.

5. Right now, the top of this file looks like this:

<?php

include("zipcodes.php");

$zip=$_POST["zip"];

//NASHVILLE//

if ($zip==$Nashville)

$my_email = "email1@mysite.com";

//Knoxville//

if ($zip==$Knoxville)

$my_email = "email2@mysite.com";

//Huntsville//

if ($zip==$Huntsville)

$my_email = "email3@mysite.com";

...etc.

This doesn't work.  It sends them all to the last option which is texas or email15@mysite.com

Before I had the first if set to if and all the others set to elseif

But that didn't work.  It sent all the emails to Nashville email I made.

If statements must work though in this way.  Because if I have this:

if ($zip==$Nashville)

$my_email = "email1@mysite.com";

     //Anything Else//

  else $my_email = "hippityhoppity@live.com";

It works.  Meaning if a zipcode is in Nashville it goes to one email - and if it isn't in Nashville - it goes to hippityhoppity.

Am I not getting something about all of this?  Thanks!

This topic has been closed for replies.
Correct answer

Arrays are useful in these type of situations:

$nashville = Array("37010","37027","37040","37055","37152");

$knoxville = Array("32010","32027","32040","32055","32152");

$huntsville = Array("47010","47027","47040","47055","47152");

if(in_array($zip, $nashville)) {

     $my_email = "......";

} elseif(in_array($zip, $knoxville) {

     $my_email = "......";

} else {

     $my_email = "....";

But if you have a lot of locations and zip codes to compare, I would suggest you to use a database like MySQL.

1 reply

Drymetal
DrymetalAuthor
Inspiring
January 28, 2012

Using the brackets doesn't work either.  All of this goes to the first email.  It doesn't make sense.  This is exactly what this php book says needs to be done. 

<?php

include("zipcodes.php");

$zip=$_POST["zip"];

if ($zip==$Nashville){

$my_email = "nashville@mysite.com";

} elseif ($zip==$Knoxville){

$my_email = "knoxville@mysite.com";

} elseif ($zip==$Huntsville){

$my_email = "huntsville@mysite.com";

} elseif ($zip==$Florida){

$my_email = "florida@mysite.com";

} elseif ($zip==$Georgia){

$my_email = "georgia@mysite.com";

} elseif ($zip==$SouthCarolina){

$my_email = "scarolina@mysite.com";

} elseif ($zip==$NorthCarolina){

$my_email = "ncarolina@mysite.com";

} elseif ($zip==$Pennsylvania){

$my_email = "penn@mysite.com";

} elseif ($zip==$Maryland){

$my_email = "maryland@mysite.com";

} elseif ($zip==$Virginia){

$my_email = "virginia@mysite.com";

} elseif ($zip==$Texas){

$my_email = "texas@mysite.com";

} else { $my_email = "hippityhoppity43204@live.com";

}

Correct answer
January 28, 2012

Arrays are useful in these type of situations:

$nashville = Array("37010","37027","37040","37055","37152");

$knoxville = Array("32010","32027","32040","32055","32152");

$huntsville = Array("47010","47027","47040","47055","47152");

if(in_array($zip, $nashville)) {

     $my_email = "......";

} elseif(in_array($zip, $knoxville) {

     $my_email = "......";

} else {

     $my_email = "....";

But if you have a lot of locations and zip codes to compare, I would suggest you to use a database like MySQL.

Drymetal
DrymetalAuthor
Inspiring
January 28, 2012

Figures you'd answer the same time I came up with it.  Would've saved me a 12 pack of beer and frustration. 

Though it makes me excited to see that my code looks like yours.  : ) 

You say to use a database instead.  Why?  Wouldn't that put more strain on the server or am I wrong?

How would I do that?  With recordsets?