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 6590181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:16:04+00:00 2026-05-25T17:16:04+00:00

This is more of a style question. I have a template file header.php in

  • 0

This is more of a style question. I have a template file header.php in which I define a PrintHeader() function.

Callers of this function can specify, via global variables, the title of the page and any Javascript scripts to include when printing the header (because surely not every page will have the same title or want to include the same scripts). I chose to use global variables rather than function arguments because the latter would require the interface to change when adding new arguments.

Is this considered “good” style, and is there a “better” way to do what I’m trying to do?

header.php (simplified)

<?php
function PrintHeader()
{
  global $pageTitle, $scripts; // Set by the caller of this function

  echo <<<HEADER
<html>
 <head>
  <title>$pageTitle</title>

HEADER;

  if( !empty($scripts) )
  {
    foreach($scripts as $script)
    {
      echo "  <script type=\"text/javascript\" src=\"$script.js\"></script>\n";
    }
  }

  echo " </head>\n";
}
?>

index.php (simplified)

<?php
  $pageTitle = 'Welcome';
  $scripts = array('script1', 'script2');
  require('header.php');
  PrintHeader();

  // Print the rest of the page
?>
  • 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-25T17:16:04+00:00Added an answer on May 25, 2026 at 5:16 pm

    is there a “better” way to do what I’m trying to do?

    sure.
    I see no point in defining and calling a function at all. as well as in using heredoc.

    header.php (dramatically simplified):

    <html>
     <head>
      <title><?=$pageTitle?></title>
    
    <? if( !empty($scripts) ): ?>
      <?  foreach($scripts as $script): ?>
    <script type="text/javascript" src="<?=$script?>.js"></script>
      <? endforeach ?>
    <? endif ?>
      </head>
    

    index.php:

    <?php
      $pageTitle = 'Welcome';
      $scripts = array('script1', 'script2');
      require('header.php');
    ?>
    

    but still it’s not the best way, as it seems you’re not using a template where it most valuable – to output page contents itself.

    So, I’d make it in three parts:

    links.php (simplified):

    <?
    //include our settings, connect to database etc.
    include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
    //getting required data
    $DATA = getdata("SELECT * FROM links");
    $pagetitle = "Links to friend sites";
    //etc
    //and then call a template:
    $tpl = "links.tpl.php";
    include "main.tpl.php";
    ?>
    

    where main.tpl.php is your main site template, including common parts, like header, footer, menu etc:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>My site. <?=$pagetitle?></title>
    </head>
    <body>
    <div id="page">
    <? include $tpl ?>
    </div>
    </body>
    </html>
    

    and finally links.tpl.php is the actual page template:

    <h2><?=$pagetitle?></h2>
    <ul>
    <? foreach($DATA as $row): ?>
    <li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
    <? endforeach ?>
    <ul>
    

    notice native HTML syntax, which is highlighted, readable and centralized in one place instead of being split between numerous functions and files

    The point is in having separate template for the every PHP page as well as main site template for them all. With such setup you’ll get a lot of advantages such as custom error pages, multiple representations of the same data (say, HTML, JSON or XML) by switching only templates without changing the code and many more

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

Sidebar

Related Questions

This is more a question of coding style, but I have a script that
This is more of a business-oriented programming question that I can't seem to figure
This is more a style question. For CPU bound processes that really benefit for
This isn't a style question. Its more about the proper use of the language
I think this question is more of a coding style rather than technical issue.
This is more of a style question, I'm wondering what other people do. Let's
So, this is a question of coding style more or less. I'm using Bean
This is a question about style and design rather than syntax. I have domain
To make this more clear, I'm going to put code samples: $file = fopen('filename.ext',
Ok, I'm going to try to make this more clear because my last question

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.