0

I am attempting to make a simple system that will allow the title of a page to be controlled through a variable called $pagetitle. In this example, I specify the variable in index.php, and then I try to utilize that variable in an included file named make_header.php. I'm not having any success in getting make_header.php to recognize the variable. From my understanding, the included file should be able to recognize the variable since I created it before using include(), but that does not seem to be the case in this situation. Would anyone know why this problem is occurring and how to make the variable work between the two files?

index.php

<?php
$pagetitle = "Home";
include('make_header.php');
makeHeader();
?>

make_header.php

<?php
function makeHeader() {
echo '<title>'. $pagetitle .'</title>';
}
?>

*Note that there is more content in the makeHeader() function that all functions properly and creates the logo, etc., but I've omitted it here as it should not have any effect on this particular problem.

Update

Below is the full makeHeader() function. While I do not believe the rest of the info in it is contributing to the problem, it couldn't hurt to check.

<?php
function makeHeader() {
echo '<title>'. $pagetitle .'</title>';
echo '<img id="logo" src="pictures/logo.png" align="left" width="842" height="182" alt="Logo"/>';
echo '<br />';
echo '<div id="time">The current time is ' . date("h:i") .  date("Y/m/d");
echo '</div>';
}
?>
ElevenQ
  • 13
  • 3

1 Answers1

2

That's because in makeHeader() function, it's trying to find a local variable named $pagetitle, not the global variable named $pagetitle. And that's why the function is unable to take $pagetitle = "Home"; into account.

So the solution is, either make the $pagetitle variable global in the function or explicitly pass it's value to the function.

Method(1):

function makeHeader() {
    global $pagetitle;
    echo '<title>'. $pagetitle .'</title>';
}

Method(2):

function makeHeader($pagetitle) {
    echo '<title>'. $pagetitle .'</title>';
}

makeHeader("Home");

Sidenote: As @JohnConde mentioned the comment below, usage of global in the code is a bad programming practice. Here's a good read on why Globals are evil.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Both methods you mention do get rid of the error I was receiving, but the title of my page still remains as the default "untitled document". Any ideas on why the variable is not showing properly? – ElevenQ May 06 '17 at 20:01
  • @ElevenQ Both are the methods are working fine on my end i.e these are showing expected title on the browser window. Please debug this issue further on your end, probably there's more to the story since you said *Note that there is more content in the makeHeader() function that all functions properly and creates the logo, etc., but I've omitted it here ...* – Rajdeep Paul May 06 '17 at 20:08
  • I wouldn't even bother mentioning using `global` as it is not a good programming practice. – John Conde May 06 '17 at 20:11
  • @JohnConde Yeah, I know. I just wanted OP to know both the methods. Give me few moments, I'll add this information in the answer as well. – Rajdeep Paul May 06 '17 at 20:15
  • I'll update the post with a bit more information to see if that may clarify the problem. I'll continue looking on my end to see if I can find the problem too. – ElevenQ May 06 '17 at 20:22
  • I think I figured it out. I have a number of other include() files. Once I removed the tags from them, it works. Didn't realize that would cause issues. All fixed now though. Thanks for the help! – ElevenQ May 06 '17 at 20:34
  • @ElevenQ Glad I could help. Cheers! :-) – Rajdeep Paul May 06 '17 at 20:36