我如何在这种情况下使用if / then / else?

I am having problem using the if/then/else statements.

here is my context:

$email = trim($_POST['email']);
$name = trim($_POST['name']);
$lname = trim($_POST['lname']);
$order = trim($_POST['order']);
$tel = trim($_POST['tel']);
$comments = trim($_POST['comments']);
$op ="0000-0000-0000";
$om ="mail@mail.com";
$bronze = "180 EGP/yr";
$silver ="280 EGP/yr";
$gold ="350 EGP/yr";
$plat ="420 EGP/yr";
$free ="EGP/yr";


if ($order == 'bronze') {
    echo "Please notice that your order will cost $bronze";
}

Now how can I do it again with $silver ? And so on like for silver, gold, plat and free or even if I wanna add more.

The switch statement would actually be a better fit for this situation:

<?php
$email = trim($_POST['email']);
$name = trim($_POST['name']);
$lname = trim($_POST['lname']);
$order = trim($_POST['order']);
$tel = trim($_POST['tel']);
$comments = trim($_POST['comments']);
$op ="0000-0000-0000";
$om ="mail@mail.com";
$bronze = "180 EGP/yr";
$silver ="280 EGP/yr";
$gold ="350 EGP/yr";
$plat ="420 EGP/yr";
$free ="EGP/yr";


switch ($order) {
  case  'bronze':
    echo "Please notice that your order will cost $bronze";
    break;
  case  'silver':
    echo "Please notice that your order will cost $silver";
    break;
  case  'gold':
    echo "Please notice that your order will cost $gold";
    break;
  case  'plat':
    echo "Please notice that your order will cost $plat";
    break;
  case  'free':
    echo "Please notice that your order will cost $free";
    break;
  default:
    echo "Please choose a service package.";
    break;
}
<?php

if($order == "bronze")
{
   echo "Please notice that your order will cost $bronze";
}
else if($order == "silver")
{
   echo "Please notice that your order will cost $silver";
}
...
?>

The most easiest google search would've brought you this.

I think you should look into the switch statement, this will allow you to perform different actions based on the value of $order.

You can do this using a variable variable:

if(!isset($$order)) {
    die("Invalid order type");
}

$cost = $$order;
echo "Please notice that your order will cost $cost";

See it in action.

This will work correctly for any number of order types, without needing you to specify them again, and will also detect an invalid type.

WARNING: However, there is a security issue with this approach as Another Code correctly points out; a user could exploit this by crafting special values for $order that would cause your script to reveal the contents of any variable, which can be a pretty significant information leak.

Preferably: the same could be achieved in a more standard manner if you used an array for the order types, and this solution does not have any security issues:

$types = array(
    'bronze' => '180 EGP/yr',
    'silver' => '280 EGP/yr',
    // etc
);

if(!isset($types[$order])) {
    die("Invalid order type");
}

echo "The cost will be ".$types[$order];

You can use the switch ... case structure.

But if you have to do quite simple things, you can even refactor a lot using associative arrays.

Example:

$minerals = array(
  'gold' => array(
    'color' => 'yellow',
    'cost' => 350
  ),
  'silver' => array(
    'color' => 'gray',
    'cost' => 280
  )
);

// check order type
if (!isset($minerals[$order]))
  die("Unsupported mineral type ".$order.".");

echo "Please notice that your order will cost "
  . $minerals[$order]['cost'] . " EGP/yr.";

Is this what you meant?

if ( $gold == 'something' ) {
    echo "Please notice that your order will cost $bronze";
}

if ( $silver == 'something' ) {
    echo "Please notice that your order will cost $bronze";
}

Hope this helps

<?php
$key=0;
switch ($order) {
    case "bronze":
         $key=$bronze; 
          break;
    case "silver":
          $key=$silver;
           break;
    case "gold":
          $key=$gold;
          break;
    default:
         $key=1;
          break;
}

echo"Please notice your order will cost $key amount";
?>

A very ugly way to do it would be like this:

echo 'Please notice that your order will cost '.$$order;

However, I don't recommend you doing it this way as it may cause lots of errors and security risk.

I would do it with an array:

$plan = array(
    'bronze' => '180 EGP/yr',
    'silver' => '280 EGP/yr',
    'gold'   => '350 EGP/yr',
    'plat'   => '420 EGP/yr',
    'free'   => 'EGP/yr'
);

if(!array_has_key($order, $plan))){
    die('unknown plan');
}else{
    echo 'Please notice that your order will cost '.$plan[$order];
}

That way it's very easy to add new plans, even dynamically or from db.