0

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.

3
  • 1
    A more complicated way is using the output buffer, ob_start(), print out everything then $var = ob_get_clean(); although it may be more complicated than you may need Commented Nov 18, 2015 at 15:57
  • And when you use return in the switch statement, you really do not need a break! Commented Nov 18, 2015 at 15:59
  • Note: you can put the cases in an array for lookup. Commented Nov 18, 2015 at 15:59

5 Answers 5

3

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>";
Sign up to request clarification or add additional context in comments.

1 Comment

Voted because the HTML is generated at the end end closing the <div> tag once.
2

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;

Comments

2

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

Comments

2

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

2 Comments

I agree with this answer, however mentioning things like performance for things like this is counter productive and misses the point completely.
I'm accord with @PeeHaa
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.