2
\$\begingroup\$

In the snippet below, I'm creating a banner for my website. If I change the variable in the html file, I want to call a different banner. It does work as is.

<?php $pencilbanner='hol'; include($_SERVER['DOCUMENT_ROOT'] . '/includes/banner.html'); ?>

In my banner.html file, I'm holding all the banners. Each one with its own variable name.

<style type="text/css">
  <?php include($_SERVER['DOCUMENT_ROOT'] . '/dist/css/components/banner.css');?>
</style>

<div class="banner <?php if($pencilbanner == 'hol') echo 'holiday-banner' ; else echo 'standard-banner'?>">
  <?php if($pencilbanner == 'std'){ ?>
    <!-- Standard -->
    <h4>This is the standard banner</h4>
  <?php } ?>

  <?php if($pencilbanner == 'hol'){ ?>
    <!-- Holiday -->
    <h4>This is the holiday banner</h4>
  <?php } ?>

  <?php if($pencilbanner == 'lim'){ ?>
    <!-- Limited -->
    <h4>This is the limited time only banner</h4>
  <?php } ?>
</div>

The issue I'm having is in this line:

<div class="banner <?php if($pencilbanner == 'hol') echo 'holiday-banner' ; else echo 'standard-banner'?>">

At worst, this can end up being an endless chain of if/else statements, based on the banner I call. Ultimately, we intend on having multiple banners, and that can lead to if else > else if > else if > else if > else if > else, and I don't want to get there.

What is the most direct approach to this without creating a long if/else statement?

\$\endgroup\$
3
  • \$\begingroup\$ Why was my informative comment removed? It contained a relevant link to a source of pre-existing advice. \$\endgroup\$ Dec 23, 2021 at 0:22
  • \$\begingroup\$ Great question. I didn't remove it. You can put it back if you want. \$\endgroup\$
    – Millhorn
    Dec 23, 2021 at 0:24
  • \$\begingroup\$ codereview.stackexchange.com/a/272205/141885 same exact advice here. If CodeReview ever closed duplicate questions, your question would be a good candidate. \$\endgroup\$ Dec 23, 2021 at 0:32

1 Answer 1

2
\$\begingroup\$

$pencilbanner can only contain one value (and you aren't changing it), so using if over and over is most inappropriate and even using elseif would be an improvement.

Using a switch block is an appropriate technique since it only evaluates the variable once. Personally, I try to avoid switch because I find all of the breaks to be ugly and verbose.

You could use a lookup array with keys like hol, etc. then associate the key with a subarray of the data you require. If the $pencilbanner is not represented in the lookup array as a key, then coalesce to a fallback array of default values.

The most modern technique (available since PHP8), is match. It is compact and elegant. Use its returned array to dynamically populate the desired HTML.

Suggested code: (Demo)

$bannerData = match($pencilbanner) {
    'std' => ['standard-banner', 'This is the standard banner'],
    'hol' => ['holiday-banner', 'This is the holiday banner'],
    'lim' => ['standard-banner', 'This is the limited time only banner'],
    default => [],
};

if ($bannerData) {
    vprintf('<div class="banner %s"><h4>%s</h4></div>', $bannerData);
}

Output with $bannerData = 'hol'; *I added tabbing:

<div class="banner holiday-banner">
    <h4>This is the holiday banner</h4>
</div>

With this technique you will only need to add a new line of code in the match body -- never needing to touch the vprintf() code.

P.s. I'd probably name your file containing PHP as banner.php instead of banner.html.

\$\endgroup\$
2
  • \$\begingroup\$ Sorry for the delayed response... holidays and all. In the area "This is the standard banner", etc... can HTML markup be added there? \$\endgroup\$
    – Millhorn
    Jan 5, 2022 at 14:38
  • \$\begingroup\$ Of course, you can add HTML mark up within the $bannerData declaration -- there are not hard rules behind this. Just try to not repeat yourself within these lookup elements. Ideally, everything should only be written once -- this makes your code D.R.Y. (Don't Repeat Yourself). \$\endgroup\$ Jan 5, 2022 at 22:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.