IF and ELSEIF

Hi All

Cant get this to work… if $country is United Kingdom it works… if $country is any of the european countries in the second elseif it works…

it is the thrid elseif that i am having the problem with… if $country is anything other than that defined in the first two if’s it still shows it as european postage…

hope this makes sense…

any ideas…
thanks
steve



IF ($country == 'United Kingdom') {
$postageprice = '0';
} ELSEIF ($country == 'Andorra' OR 'Austria' OR 'Belgium' OR 'Bulgaria' OR 'Croatia' OR 'Cyprus' OR 'Czech Republic' OR 'Denmark' OR 'Estonia' OR 'Finland' OR 'France' OR 'Germany' OR 'Greece' OR 'Hungary' OR 'Iceland' OR 'Ireland' OR 'Italy' OR 'Latvia' OR 'Lithuania' OR 'Luxembourg' OR 'Malta' OR 'Monaco' OR 'Netherlands' OR 'Norway' OR 'Poland' OR 'Portugal' OR 'Romania' OR 'Slovakia' OR 'Slovenia' OR 'Spain' OR 'Sweden' OR 'Switzerland' OR 'Turkey' OR 'Ukraine' OR 'Vatican City') {
$postageprice = $oneyeareuropepostage;
echo "</tr><tr>";
echo "<td width='250'>European Postage:</td><td>£$oneyeareuropepostage</td>";
} ELSEIF ($country != 'United Kingdom' OR 'Andorra' OR 'Austria' OR 'Belgium' OR 'Bulgaria' OR 'Croatia' OR 'Cyprus' OR 'Czech Republic' OR 'Denmark' OR 'Estonia' OR 'Finland' OR 'France' OR 'Germany' OR 'Greece' OR 'Hungary' OR 'Iceland' OR 'Ireland' OR 'Italy' OR 'Latvia' OR 'Lithuania' OR 'Luxembourg' OR 'Malta' OR 'Monaco' OR 'Netherlands' OR 'Norway' OR 'Poland' OR 'Portugal' OR 'Romania' OR 'Slovakia' OR 'Slovenia' OR 'Spain' OR 'Sweden' OR 'Switzerland' OR 'Turkey' OR 'Ukraine' OR 'Vatican City') {
$postageprice = $oneyearworldpostage;
echo "</tr><tr>";
echo "<td width='250'>World Postage:</td><td>£$oneyearworldpostage</td>";
}


I would strongly suggest you use switches instead of the long list of or’s:
http://us2.php.net/manual/en/control-structures.switch.php

This will result is both easier to read code as well as better overall performance. While it may be fractions of a second improvement, that ads up when you scale.

Review the overall logic as it really doesn’t look logical.

Thanks inclick…

changed it to this and it works perfectly…

have never used switch before…cheers…


switch($country)
{
    case 'United Kingdom';
$postageprice = '0';
    break;

      case 'Andorra';
      case 'Austria';
      case 'Belgium';
      case 'Bulgaria';
      case 'Croatia';
      case 'Cyprus';
      case 'Czech Republic';
      case 'Denmark';
      case 'Estonia';
      case 'Finland';
      case 'France';
      case 'Germany';
      case 'Greece';
      case 'Hungary';
      case 'Iceland';
      case 'Ireland';
      case 'Italy';
      case 'Latvia';
      case 'Lithuania';
      case 'Luxembourg';
      case 'Malta';
      case 'Monaco';
      case 'Netherlands';
      case 'Norway';
      case 'Poland';
      case 'Portugal';
      case 'Romania';
      case 'Slovakia';
      case 'Slovenia';
      case 'Spain';
      case 'Sweden';
      case 'Switzerland';
      case 'Turkey';
      case 'Ukraine';
      case 'Vatican City';
$postageprice = $oneyeareuropepostage;
echo "</tr><tr>";
echo "<td width='250'>European Postage:</td><td>£$oneyeareuropepostage</td>";
    break;


    default;
$postageprice = $oneyearworldpostage;
echo "</tr><tr>";
echo "<td width='250'>World Postage:</td><td>£$oneyearworldpostage</td>";
    break;
}

Glad to see that worked. Looks a lot easier to understand as well. Happy coding.

I’d stick it in a db (you probably wanna edit prices), or use arrays:


<?php
$europrice = 1;
$oceaniaprice = 2;
$postage = array(
    'Austria' => $europrice,
    'Belgium' => $europrice,
    'Australia' => $oceaniaprice,
);
?>
<td width='250'>World Postage:</td><td>£<?php echo $postage[$country] ?></td>

or even


<?php
$areas = array(
    'europe' => array('Austria', 'Belgium'),
    'oceania' => array('Australia', 'NZ'),
);
$prices = array(
    'europe' => 1,
    'oceania' => 2,
);

$price = '';
foreach($areas as $k => $v) {
    if(in_array($country, $v)) {
        $price = prices[$k];
        break;
    }
}
?>
<td><?php echo $price ?></td>