Navigation item selection Using Bootstrap 3 and PHP, I create my navigation bars in the following way:

&lt;?php
$page_url = $_SERVER['REQUEST_URI'];
$active1 = (strpos($page_url, "one.php") !== false ? " class=\"active\"" : "");
$active2 = (strpos($page_url, "two.php") !== false ? " class=\"active\"" : "");
$active3 = (strpos($page_url, "three.php") !== false ? " class=\"active\"" : "");
$active4 = (strpos($page_url, "four.php") !== false ? " class=\"active\"" : "");
?&gt;
&lt;div class="navbar navbar-default"&gt;
  &lt;div class="container"&gt;
    &lt;div class="navbar-header"&gt;
      &lt;button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"&gt;
        &lt;i class="icon-bar"&gt;&lt;/i&gt;
        &lt;i class="icon-bar"&gt;&lt;/i&gt;
        &lt;i class="icon-bar"&gt;&lt;/i&gt;
      &lt;/button&gt;
      &lt;a class="navbar-brand" href="index.php"&gt;Company Name&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class="collapse navbar-collapse"&gt;
      &lt;ul class="nav navbar-nav"&gt;
        &lt;li&lt;?=$active1?&gt;&gt;&lt;a href="one.php"&gt;Link One&lt;/a&gt;&lt;/li&gt;
        &lt;li&lt;?=$active2?&gt;&gt;&lt;a href="two.php"&gt;Link Two&lt;/a&gt;&lt;/li&gt;
        &lt;li&lt;?=$active3?&gt;&gt;&lt;a href="three.php"&gt;Link Three&lt;/a&gt;&lt;/li&gt;
        &lt;li&lt;?=$active4?&gt;&gt;&lt;a href="four.php"&gt;Link Four&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


Then, depending on which page you're on, a link within the navigation has class="active" applied to it. However, this feels like a very messy way of building this functionality to the &lt;navbar&gt;. Lots of empty variables at one time, and a lot of almost identical code for each navigation element.

How best can I improve on this code?
Use an array and a function

You've got quite a few pieces of code that look very similar. The more menu items you have, the worse this gets. This is almost always a sign you should start using a function. Here's how to do that:

&lt;?php

  $menuItems = array('Link One'   =&gt; 'one.php',
                     'Link Two'   =&gt; 'two.php',
                     'Link Three' =&gt; 'three.php',
                     'Link Four'  =&gt; 'four.php');

  function navigationItem($title,$page)
  {
    $class = (strpos($_SERVER['REQUEST_URI'],$page) !== false) ? ' class="active"' : '';
    return '        &lt;li'.$class.'&gt;&lt;a href="'.$page.'"&gt;'.$title.'&lt;/a&gt;&lt;/li&gt;'.PHP_EOL;
  }

?&gt;
&lt;div class="navbar navbar-default"&gt;
  &lt;div class="container"&gt;
    &lt;div class="navbar-header"&gt;
      &lt;button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"&gt;
        &lt;i class="icon-bar"&gt;&lt;/i&gt;
        &lt;i class="icon-bar"&gt;&lt;/i&gt;
        &lt;i class="icon-bar"&gt;&lt;/i&gt;
      &lt;/button&gt;
      &lt;a class="navbar-brand" href="index.php"&gt;Company Name&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class="collapse navbar-collapse"&gt;
      &lt;ul class="nav navbar-nav"&gt;
&lt;?php

  foreach ($menuItems as $title =&gt; $page) echo navigationItem($title,$page);

?&gt;      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


As you can see I've put both lines, which you repeat four times, inside the function and simply call that function four time. I could have done that by simply writing:

&lt;?php
  echo navigationItem('Link One','one.php');
  echo navigationItem('Link Two','two.php');
  echo navigationItem('Link Three','three.php');
  echo navigationItem('Link Four','four.php');
?&gt; 


but I prefer to have the menu at the top of the file in an array. This makes it easier to find and edit.

This was probably the next step you were looking for. But it should not be your last. 

You could easily generalize the navigationItem() function for all list items you will make. And the foreach loop basically builds a list, why not make a function out of that? 

After that you could make a class which could build a whole menu bar for you. The functions will become methods of that class. 

Why not put the array in a seperate file with the other stuff that defines your site? Perhaps eventually create a database table with menu items?

There are many ways in which you can improve this tiny piece of code.

What you should definately NOT do is: Build a whole site with Bootstrap HTML code interspersed by piece of loose PHP code. That will be chaos, and very difficult to maintain and expand. Be systematic about it. 

Preferably try to avoid putting piece of PHP code inside HTML pages. It's just so ugly. Personally I prefer to write in pure PHP, and generate the HTML using objects similar to the class I suggested for your menu bar.