Skip to content
Advertisement

How to use a Function of Switch/Case, if at all?

I have various products inside a database, each has its own price, the pattern to output a product price is:

$product[i]["price"]

I also have various “product types”, which are manually defined in my code, each with index number that will relate to each product accordingly:

$type1="normal"
$type2="normal"
$type3="special"
...

I want to echo a message according to the price, and to the type, for each product.

My code pattern would be something like:

<p> lorem <?php echo function_output($product[1]["price"],$type1)?><p>
<p> ipsum <?php echo function_output($product[2]["price"],$type2)?><p>
<p> done <?php echo function_output($product[3]["price"],$type3)?><p>

(lorem ipsum stuff means I have some static content within the code).

Echoed line should be:

  1. If price is 0 –> <span class="free">Free</span>

  2. If price > 0 –> Echo $ sign + the price $product[i]["price"]

  3. By default/fallback –> Don’t echo anything

  4. If the type is special, no matter what the price is –> Echo It's special

Intuitively it sounds like something to handle with switch command, but I really don’t know how to use it the way I want, which probably involves a function. I only know the most basic form of switch command, I assume that my code should be something like:

$my_text = function_output($product[i]["price"],$type);

    switch ($product[i]["price"]) {

    case $product[i]["price"]=="0":
        $message = '<span class="free">Free</span>';
        break;

    case $product[i]["price"]>"0":
        $message = '$product[i]["price"]';
        break;

    case $type1="special":
        $message = 'It's Special';
        break;

    default:
        $message = 'no info';
    }

Yes, I know it’s a complete mess, but no clue how to do this.

Edit:

For example, when $product[i]["price"]=50 and $type2="normal"

function_output($product[2]["price"],$type2) should return: $50

Edit 2:

Basically I want the function to used in a similar way to following method:

function example( $slug ) {
    $class_map = array(
        'special' => 'it's special',
        'default'    => 'nothing'
    );

    return ( isset( $class_map[ $slug ] ) ) ? $class_map[ $slug ] : $class_map[ 'default' ];
}

and then:

`example( $product[1]["price"],$type1)`
`example( $product[2]["price"],$type2)`
...

Advertisement

Answer

I’m assuming a price can never be less than zero.
So it seems that there are only three output options: “special”, “free”, or “price”.

PHP’s elseif might be more effective here:

function function_output($price=0,$type='normal') {

  if ($type == "special") {
    $message = "It's Special";
  } elseif ($price == 0) {
    $message = '<span class="free">Free</span>';
  } else {
    $message = '$'.$price;
  }

  return $message;

}

But you can also use switch to evaluate multiple variables by passing it a value of true.
This might be useful if you’re going to add more options in the future.

function function_output($price=0,$type='normal') {

    switch (true) {
      case $type == "special":
        $message = "It's Special";
        break;
      case $price == 0:
        $message = '<span class="free">Free</span>';
        break;
      default:
        $message = '$'.$price;
    }

    return $message;

}

Here’s a working example.


Edit

Just for fun, a short version using nested ternary operators:

function output($p=0,$t='normal') {
    return $t=='special'?"It's Special":($p==0?'<span class="free">Free</span>':'$'.$p);
}

Working Example

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement