Reduce if/else statements

I've written a very simple if statement to check the url. It works perfectly fine, but is there any way to trim it down?

    <?php if ($_SERVER['REQUEST_URI'] === "/jim.html") { ?>
    <a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">Inman jim <i class="fa fa-angle-down"></i></a>
    <?php } elseif ($_SERVER['REQUEST_URI'] === "/bob.html") { ?>
    <a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">bob <i class="fa fa-angle-down"></i></a>
    <?php } elseif ($_SERVER['REQUEST_URI'] === "/dereck.html") { ?>
    <a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">dereck <i class="fa fa-angle-down"></i></a>
    <?php } elseif ($_SERVER['REQUEST_URI'] === "/maxamilamatronicus") { ?>
    <a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">maxamilamatronicus <i class="fa fa-angle-down"></i></a>

It is simply to show the correct heading at the drop of a drop down menu.

Thanks!

Upvotes: 0

Views: 95

Answers (3)

webNeat
webNeat

Reputation: 2828

here is a more simple code which is giving the same result:

<?php 
$menus = [
    '/jim.html'     => [ 'title' => 'Inman jim', 'href' => '#'],
    '/bob.html'     => [ 'title' => 'bob', 'href' => '#'],
    '/dereck.html'  => [ 'title' => 'dereck', 'href' => '#'],
    '/maxamilamatronicus' => [ 'title' => 'maxamilamatronicus', 'href' => '#']
];
$menu = ['title' => 'Unknown', 'href' => '#'];
if(isset($menus[$_SERVER['REQUEST_URI']]))
    $menu = $menus[$_SERVER['REQUEST_URI']];
?>
<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="<?=$menu['href']?>">
    <?= $menu['title'] ?>
    <i class="fa fa-angle-down"></i>
</a>

Please note that the text will be "Unknown" if the $_SERVER['REQUEST_URI'] is not equal to any key in the $menus array.

Upvotes: 2

Anand
Anand

Reputation: 743

You can use switch case in PHP. Which is more efficient. Look at the example here: http://www.w3schools.com/php/php_switch.asp

Also try to keep as many items like the one below outside the PHP. So that you can improve the readability and efficiency.

<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown">

Hence the better solution would be:

<a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown"
   <?php switch ($_SERVER['REQUEST_URI']) {
        case "/jim.html" : ?>
                            href="#">Inman jim <?php
                             break;
        case "/bob.html" : ?>
                             href="#">bob <?php
                             break;
        case "/dereck.html" : ?>
                              href="#">dereck <?php
                             break;
        case "/maxamilamatronicus" : ?>
                             href="#">maxamilamatronicus <?php
                             break;

    }?>
<i class="fa fa-angle-down"></i></a>

Upvotes: 0

Aju John
Aju John

Reputation: 2244

Use Switch More readable and easy to maintain:

switch ($_SERVER['REQUEST_URI']) {
    case "/jim.html" : ?>
                         <a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">Inman jim <i class="fa fa-angle-down"></i></a><?php
                         break;
    case "/bob.html" : ?>
                         <a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">bob <i class="fa fa-angle-down"></i></a><?php
                         break;
    case "/dereck.html" : ?>
                         <a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">dereck <i class="fa fa-angle-down"></i></a><?php
                         break;
    case "/maxamilamatronicus" : ?>
                         <a class="btn btn-primary dropdown-toggle btn-gallery" data-toggle="dropdown" href="#">maxamilamatronicus <i class="fa fa-angle-down"></i></a><?php
                         break;

}

Upvotes: 1

Related Questions