0

I'm trying to set a page title dynamically using PHP, where it pulls the name of the page from a lang file, however I can't get the syntax quite right.

Example:

lang.en.php

<?php $lang['TITLE_INDEX'] = 'My Index Page'; ?>

header.php

<?php include 'lang.en.php' ?><title><?php echo $pagetitle ?></title>

index.php

<?php $pagetitle = $lang['TITLE_INDEX']; include 'header.php' ?>

It pulls the language through a common file which works out which lang to use (en,de etc), however I cann't get the $pagetitle var to output the $lang.

ah1
  • 71
  • 3
  • 9

3 Answers3

0

The issue is that you are setting the variable $pagetitle after the include. Try this:

<?php include 'lang.en.php'; $pagetitle = $lang['TITLE_INDEX']; include 'header.php'; ?>
Ravi Gehlot
  • 1,099
  • 11
  • 17
  • Thanks for your response, I've given that a go and it doesn't work. It doesn't seem to like setting $pagetitle as another $var ($pagetitle = $lang['TITLE_INDEX'];) – ah1 May 30 '17 at 13:00
  • Then you may have another issue. But my answer should point you in the right direction. I am confused as to why it was voted down. – Ravi Gehlot May 30 '17 at 13:02
  • It was downvoted because it is wrong. You have to do the include before you can utilize the variables in the included file. You're missing the include of the language file. – Jay Blanchard May 30 '17 at 13:22
  • Hi Jay, that was meant to point him in the right direction. He got back to me by saying that it didn't work. Like other answers, I follow back with the user in answering further questions to arrive at the right answer. Respectfully, my answer was immediately down voted without the chance of an update or anything else. – Ravi Gehlot May 30 '17 at 13:30
  • Respectfully it was wrong right out of the gate. We should strive to make our answers as right as possible without having to await clarification before editing. – Jay Blanchard May 30 '17 at 15:17
  • Thanks Jay. I am still learning how to use Stackoverflow effectively. I see your point. – Ravi Gehlot May 30 '17 at 15:20
0

the order of the includes is important. PHP will replace the include by the actual code exactly where the include is. Don't forget to include your lang file too, or the value will not be set:

index.php

<?php
include 'lang.en.php';
$pagetitle = $lang['TITLE_INDEX'];
include 'header.php';
?>

note: of course, you will replace the en in your lang import by your locale dynamically

Kaddath
  • 5,933
  • 1
  • 9
  • 23
  • Thanks, I've give that a go - it seems '$pagetitle = $lang['TITLE_INDEX'];' does not work – ah1 May 30 '17 at 13:07
  • do you have an error displayed somehow? you might need to set $lang as an array first, before adding a row, with `$lang = array();` in lang.en.php – Kaddath May 30 '17 at 13:11
  • No error, it just returns blank. The lang.en.php file is an array, and it works if I , just not when trying to set it as a $var – ah1 May 30 '17 at 13:17
  • 1
    you get a blank title or a whole blank page? in the second case, it might be an error but that your configuration does not allow to show. does adding `ini_set('display_errors', 1); ini_set('error_reporting', E_ALL);` at the very beginning of the code does something? (at the beginning means before any include) – Kaddath May 30 '17 at 13:23
  • It just returns a blank title, the rest works, unfortunately no errors. It just seems that you can't do $var = $var1 – ah1 May 30 '17 at 15:16
  • If you echo $lang['TITLE_INDEX'] inside of header.php, do you get the title? If you do the same within the index.php, do you get the title? – Ravi Gehlot May 30 '17 at 15:29
  • from the clues i have, there are few things i can figure to go wrong, except a typo that was not copied here, or a scope problem (if you do the import in a function, or a framework file that is included in a function). If the latter, you can try to set the vars as `global`. The rest is simple, it should work – Kaddath May 30 '17 at 15:53
  • If I echo it within header.php or index.php it returns fine, just not if setting it as a var – ah1 May 30 '17 at 20:38
0

I think you forgot to include lang.en.php. The basic file structure should look like this:

lang.en.php:

$lang = [];
$lang['TITLE_INDEX'] = 'My Index Page';

header.php:

$usedLang = "en";
include_once("lang.$usedLang.php");

index.php:

include_once("header.php");

echo $pagetitle;

Take a look here, to find the default language https://stackoverflow.com/a/30300728/1152471

PKeidel
  • 2,559
  • 1
  • 21
  • 29