Home > Software engineering >  Better way for PHP Switch Statement with Condition operator in each Case? Optimization
Better way for PHP Switch Statement with Condition operator in each Case? Optimization

Time:05-19

I am developing a template for a WordPress page. Users have levels (beginner, amateur, intermediate, and advanced). My need was to load a % of content based on the level, with each higher level increasing the % of content shown.

As a novice, I wanted to work only in page-template.php. Alternative syntax lets you put HTML markup (without include and external .html files) between php tags. Next, the switch statement has fallthrough functionality to "load" content based on their level.

After reading more on switch, comparison operators in switch, heredoc, and alternative syntax, I still wasn't able to find what worked for me, so I wanted to share this to future novice programmers some time :p

Here is a functional piece of code that achieves what I want:

<?php $level = 1; // enter user level here ?>
<?php switch(true): ?>
<?php case ($level >= 1): ?> 

    <div>
        1 <!-- represents 25% of content -->
    </div>

<?php if($level == 1){break;} ?>
<?php case ($level >= 2): ?> // ugly code

    <div> 
        2 <!-- represents 25% of content -->
    </div>

<?php if($level == 2){break;} ?> // ugly code
<?php case ($level >= 3): ?> 

    <div>
        3 <!-- represents 25% of content -->
    </div>

<?php if($level == 3){break;} ?> // ugly code
<?php case ($level >= 4): ?>

    <div>
        4 <!-- represents 25% of content -->
    </div>

<?php if($level == 4){break;} ?> // ugly code
<?php endswitch; ?>

That is, with each level, you get 25% more content until level 4... where all content is available. You will notice I have conditional breaks. This is because the conditionals in the cases don't seem to work alone as you one would hope -- I wasn't able to figure out why, but the break with the conditional help. It's redundant but it's the only thing I got to work.

For future readers, be wary of the spacing and tabination. Alternative syntax is very particular about it, and it may frustrate you if you neglect it.

So my question is if there is another way to improve this code or a cleaner way to write it? While writing this, I realize I can probably use alternative syntax for the breaks.

CodePudding user response:

Maybe you can consider using a for loop?

<?php 
$content[1] = "Content A";
$content[2] = "Content B";
$content[3] = "Content C";
$content[4] = "Content D";

$level = 3; // enter user level here 

for ($lvl = 1; $lvl <= $level; $lvl  ) { ?> 
    <div>
        <?php echo $content[$lvl]; ?> <!-- represents 25% of content -->
    </div>
<?php } ?>

CodePudding user response:

I wouldn't use switch for this, but just a series of if statements:

<?php $level = 2; // enter user level here ?>

<?php if ($level >= 1) { ?>
    <div> 1 <!-- represents 25% of content --> </div>
<?php } if ($level >= 2) { ?>
    <div> 2 <!-- represents 25% of content --> </div>
<?php } if ($level >= 3) { ?>
    <div> 3 <!-- represents 25% of content --> </div>
<?php } if ($level >= 4) { ?>
    <div> 4 <!-- represents 25% of content --> </div>
<?php } ?>

An alternative is to first store all the content in an array, and then output a slice of that array:

<?php 

$level = 2;

$content = [
<<<TEXT
    <div> 1 <!-- represents 25% of content --> </div>
TEXT
,
<<<TEXT
    <div> 2 <!-- represents 25% of content --> </div>
TEXT
,
<<<TEXT
    <div> 3 <!-- represents 25% of content --> </div>
TEXT
,
<<<TEXT
    <div> 4 <!-- represents 25% of content --> </div>
TEXT
];

echo implode("\n", array_slice($content, 0, $level));
?>
  • Related