Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 816021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:46:11+00:00 2026-05-15T01:46:11+00:00

I want to put my html navigation in a separate php file so when

  • 0

I want to put my html navigation in a separate php file so when I need to edit it, I only have to edit it once. The problem starts when I want to add the class active to the active page.

I’ve got three pages and one common file.

common.php :

<?php 
$nav = <<<EOD
   <div id="nav">
        <ul>
           <li><a <? if($page == 'one'): ?> class="active"<? endif ?> href="index.php">Tab1</a>/</li>
           <li><a href="two.php">Tab2</a></li>
           <li><a href="three.php">Tab3</a></li>
       </ul>
    </div>
EOD;
?>

index.php :
All three pages are identical except their $page is different on each page.

  <?php
     $page = 'one';      
     require_once('common.php');
    ?>
    <html>
       <head></head>
       <body>
          <?php echo $nav; ?>
       </body>
    </html>

This simply won’t work unless I put my nav on each page, but then the whole purpose of separating the nav from all pages is ruined.

Is what I want to accomplish even possible? What am I doing wrong?

Thanks

EDIT: When doing this, the php code inside the li don’t seem to run, it’s just being printed as if it was html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:46:12+00:00Added an answer on May 15, 2026 at 1:46 am

    Your index.php code is correct. I am including the updated code for common.php below then I will explain the differences.

    <?php 
         $class = ($page == 'one') ? 'class="active"' : '';
         $nav = <<<EOD
            <div id="nav">
                <ul>
                   <li><a $class href="index.php">Tab1</a>/</li>
                   <li><a href="two.php">Tab2</a></li>
                   <li><a href="three.php">Tab3</a></li>
               </ul>
            </div>
     EOD;
     ?>
    

    The first issue is that you need to make sure that the end declaration for your heredoc — EOD; — is not indented at all. If it is indented, then you will get errors.

    As for your issue with the PHP code not running within the heredoc statement, that is because you are looking at it wrong. Using a heredoc statement is not the same as closing the PHP tags. As such, you do not need to try reopening them. That will do nothing for you. The way the heredoc syntax works is that everything between the opening and closing is displayed exactly as written with the exception of variables. Those are replaced with the associated value. I removed your logic from the heredoc and used a tertiary function to determine the class to make this easier to see (though I don’t believe any logical statements will work within the heredoc anyway)

    To understand the heredoc syntax, it is the same as including it within double quotes (“), but without the need for escaping. So your code could also be written like this:

    <?php 
         $class = ($page == 'one') ? 'class="active"' : '';
         $nav = "<div id=\"nav\">
                <ul>
                   <li><a $class href=\"index.php\">Tab1</a>/</li>
                   <li><a href=\"two.php\">Tab2</a></li>
                   <li><a href=\"three.php\">Tab3</a></li>
               </ul>
            </div>";
     ?>
    

    It will do exactly the same thing, just is written somewhat differently. Another difference between heredoc and the string is that you can escape out of the string in the middle where you can’t in the heredoc. Using this logic, you can produce the following code:

    <?php 
         $nav = "<div id=\"nav\">
                <ul>
                   <li><a ".(($page == 'one') ? 'class="active"' : '')." href=\"index.php\">Tab1</a>/</li>
                   <li><a href=\"two.php\">Tab2</a></li>
                   <li><a href=\"three.php\">Tab3</a></li>
               </ul>
            </div>";
     ?>
    

    Then you can include the logic directly in the string like you originally intended.

    Whichever method you choose makes very little (if any) difference in the performance of the script. It mostly boils down to preference. Either way, you need to make sure you understand how each works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of html I want to put into a database field:
I have a CellTable where I want to put HTML-code in the cells. The
I want put tracking pixel on 3rd party html file, which will call a
In my home page template Index.html.twig I have one navigation bar which i only
Can somebody please help me, I want to put the OK symbol throw HTML
I want to pass the parameter which is rentalPeriod using Html.BeginForm. Currently, I put
hey, hi i want put limit on object creation means a class can have
I want to put two side by side on another (I have three )
I want to put the apk file into the Kindle Fire storage(through USB cable)
I want to put breakpoint on all functions of a file. I came across

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.