CyberJunkie
CyberJunkie

Reputation: 22674

PHP - Add variable to switch statement value

I have the following switch statement:

$html = '<div class="'. $some_value . '">';

switch ($some_value) {
    case "one":
        return $html . 'One Biscuit</div>';
        break;
    case "two":
        return $html . 'Two Chimps</div>';
        break;
    case "three":
        return $html . 'Three Pies</div>';
        break;
    default:
        return $html . 'Meh...</div>';
}

Noticed how I added the $html variable to each case? Not good... Is it possible to add it just once to the final value of the switch statement? I'm trying to wrap the final value in the dynamic HTML.

Upvotes: 0

Views: 1797

Answers (5)

miglio
miglio

Reputation: 2058

Other ways is saving data in an array:

$some_value = 'two';
//
$data = array(//this data could be stored in a database table
  'one'  => 'One Biscuit',
  'two'  => 'Two Chimps',
  'three'=> 'Three Pies',
  'default' => 'Meh...'
);

$html = '<div class="'.$some_value.'">'.(isset($data[$some_value])?$data[$some_value]:$data['default']).'</div>';
var_dump($html);

result:

string '<div class="two">Two Chimps</div>' (length=33)

In some cases array is faster than switch : In PHP what's faster, big Switch statement, or Array key lookup

Upvotes: 2

FirstOne
FirstOne

Reputation: 6217

One way to do it:

$html = '<div class="'. $some_value . '">';
$v = 'Meh...</div>';
switch ($some_value) {
    case "one":
        $v = 'One Biscuit</div>';
        break;
    case "two":
        $v = 'Two Chimps</div>';
        break;
    case "three":
        $v = 'Three Pies</div>';
        break;
}

$html .= $v;

Since you are using return, you can just return once in the end: return $html.$v

Also, you could define a param to the default value, like so:

function someFunction(DUNNO_YOUR_PARAMS, $v = 'Meh...'){
    $v .= '</div'>;
    // rest of code

Upvotes: 2

MiltoxBeyond
MiltoxBeyond

Reputation: 2731

Another solution which may be easier to maintain if your html is longer:

<?php
ob_start();
echo '<div class="'.$some_var.'">';
/*
Be sure the file exists in the location you want
You can change items to the name of any directory you want just be
sure the file exists in the end. In the case of linux be sure the file
is the exact same name...
*/
include('items/'.$some_var.'.php');
echo '</div>';
$output = ob_get_clean();
return $output;
?>

In your files you just put the html code you want.
Example three.php (it really is just the html or plain text unless you process something:

Three pies

This can probably be refactored if you have too many files that are this simple. But in the case it is more complex you can include more stuff.

Upvotes: 0

CapitanFindus
CapitanFindus

Reputation: 1526

What about this:

switch($some_value){
    case 'one':
        $var="One Biscuit";
    break;
    case 'two':
        $var="Two Chimps";
    break;
    case 'three':
        $var="Three Pies";
    break;
    default:
        $var="Meh...";
    break;
}
$html="<div class=".$some_value.">".$var."</div>";

Upvotes: 3

Jan
Jan

Reputation: 43169

Store the string in new variable. Also you do not need a break after a return statement.

$html = '<div class="'. $some_value . '">';

$str = null;
switch ($some_value) {
    case "one":
        $str = 'One Biscuit</div>';
        break;
    case "two":
        $str = 'Two Chimps</div>';
        break;
    case "three":
        $str = 'Three Pies</div>';
        break;
    default:
        $str = 'Meh...</div>';
        break;
}
return $html.$str;

Upvotes: 2

Related Questions