2

I am new to PHP. I have these 3 files :

  • index.php
  • functions.php (to organize functions)
  • header.php

I want to simplify(which has been done so far) the index.php page thus I do not need to write the and all stuff again and again. So I created header.php that can be loaded by index.php:

header.php

<!Doctype html>
<html lang="en">

<head>
    <title>Learn PHP</title>  <!--This is the problem, every page that loads header.php will have same title! -->
</head>

<body>
<div class="header">

    <h1>Learn PHP</h1>
    <p>Your library of PHP learning!</p>
    <hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->

I have even simplified things further by making a function in functions.php so that I can just type "get_header()" in the index.php without writing the whole code again.

functions.php

    <?php

function get_header(){
        if (file_exists('header.php')){
                require 'header.php';
            }
            else{
                echo "There is an error retrieving a file";
            }   
    }

?>

Now, how do I allow this index.php to have custom page title instead of the default given by header.php?

Am I missing something important. I have tried creating a variable and try to pass it to the functions.php, but it didn't work. Or is there any cleaner way to do this?

I am inspired by how wordpress organize their files, I have checked the wordpress file. And then I decided to try something from scratch so I understand better and improve my PHP skills.

I know can use POST and GET, but no I dont want to refresh or load a new page just to change a page title especially index.php

EDIT :

Here I included my index.php

<?php
    require 'functions.php';
?>
<?php 
    get_header();
?>  

<table>
    <h3>What we learned</h3>

    <ul>
        <li><a href="sample">Syntax</a>         </li>
        <li><a href="sample">Variables</a>      </li>
        <li><a href="sample">Code Flow</a>      </li>
        <li><a href="sample">Arrays</a>         </li>
        <li><a href="sample">Superglobals</a>   </li>

    </ul>
</table>

<?php get_footer(); ?>
Mohd.Zafranudin
  • 364
  • 4
  • 12
  • use ajax to pass variables without page refresh – Ravi Roshan Jul 23 '16 at 09:23
  • I know what you mean, but this happens before the page even load. There is no need for AJAX, unless I demand the user to click on something, which is no, because I try to automate this from server side. – Mohd.Zafranudin Jul 23 '16 at 10:47

2 Answers2

1

It seems like all you need you want is simple includes. You're actually making it harder by using a function, here, because an include has the same scope as where it was included from. E.g.

header.inc

…
<title><?php echo isset($title) ? $title : 'Untitled';?></title>
…

index.php

<?php
$title = 'Welcome';
require 'header.inc';
?>
welcome

another-page.php

<?php
$title = '2nd page';
require 'header.inc';
?>
2nd page content

If you want to use a function, give it parameters.

function get_header($title = 'Some default title') {
    …
}

the included file will have access to the variables in the function's scope.

Walf
  • 8,535
  • 2
  • 44
  • 59
0

in the functions.php

function get_header(){
    if (file_exists('header.php'))
      require 'header.php';
    else echo "There is an error retrieving a file";   
}

in the header.php, and in the balise title you call the session parameter

<!Doctype html>
<html lang="en">

<head>
    <title>
     <?php if(!empty($_SESSION['title-page'])) echo $_SESSION['title-page']; else  'Learn PHP'; ?>
    </title>
</head>

<body>
<div class="header">

    <h1>Learn PHP</h1>
    <p>Your library of PHP learning!</p>
    <hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->

and in the index.php

<?php 
    session_start(); 
    $_SESSION['title-page'] = 'this is the welcome Page'; 
    require 'functions.php'; 
    get_header(); 
?>

<table>
    <h3>What we learned</h3>

    <ul>
        <li><a href="sample">Syntax</a>         </li>
        <li><a href="sample">Variables</a>      </li>
        <li><a href="sample">Code Flow</a>      </li>
        <li><a href="sample">Arrays</a>         </li>
        <li><a href="sample">Superglobals</a>   </li>
    </ul>
</table>

<?php get_footer(); ?>

and in another-page.php

<?php 
    session_start(); 
    $_SESSION['title-page'] = 'this is an another Page'; 
    require 'functions.php'; 
    get_header(); 
?>

<table>
    <h3>What we learned</h3>

    <ul>
        <li><a href="sample">Syntax</a>         </li>
        <li><a href="sample">Variables</a>      </li>
        <li><a href="sample">Code Flow</a>      </li>
        <li><a href="sample">Arrays</a>         </li>
        <li><a href="sample">Superglobals</a>   </li>
    </ul>
</table>

<?php get_footer(); ?>
Fahmi B.
  • 758
  • 2
  • 10
  • 23
  • I'm sorry but I dont quite understand how this works. Besides, the header.php is also being loaded by index.php. index.php > header.php > functions.php – Mohd.Zafranudin Jul 23 '16 at 08:55
  • No it did not, it throws an error require(header.php?title=title): failed to open stream. I think im missing something, but I did put the params at get_header("title") too – Mohd.Zafranudin Jul 23 '16 at 09:02
  • @Mohd.Zafranudin, what is the result ? – Fahmi B. Jul 23 '16 at 09:28
  • 1
    Yes it works, and this is the logic that I want to achieve, and I understand a lot by looking at the code. Thank you. session_start() make sense to me now – Mohd.Zafranudin Jul 23 '16 at 10:42
  • That is not what session variables are for. They're for persisting variables between separate page loads, not within a single page load. – Walf Jul 23 '16 at 11:21
  • If you're not going to use parameters in `get_header()`, which is [a bad design pattern](http://stackoverflow.com/a/2216355/315024), at least use `$GLOBALS` instead of starting a session. – Walf Aug 04 '16 at 01:05