Martin Lovecký
Martin Lovecký

Reputation: 21

Undefined variable $title - PHP

I have file index.php and I am trying echo $title inside head.php like this.

<title><?php if(isset($title)){ echo $title; }?></title>

For some reason PHP return Notice: Undefined variable: title in /includes/head.php on line 7
NULL.

So my first idea was to try var_dump($title); inside index.php and indeed I get my title but than I write var_dump($title); to head.php and its NULL and I dont know why. I am ussing this solution for title long time and I never had this problem. [and yes I tried write title to index.php still no luck].
This is my index.php

include(DIR . "/includes/head.php");    // Styles
include(DIR . "/includes/menu.php");
include(DIR . "/includes/generate.php"); // title is from here.
include(DIR . "/includes/footer.php");

EDIT: This is head.php

<!DOCTYPE html>
<html>

    <head>
      <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title><?php if(isset($title)){ echo $title; }?></title>
        <link rel="stylesheet" href="/assets//bootstrap/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat:400,400i,700,700i,600,600i">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Audiowide&amp;subset=latin-ext">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/typicons/2.0.9/typicons.min.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/simple-line-icons/2.4.1/css/simple-line-icons.min.css">
        <link rel="stylesheet" href="/assets/css/Login-Form-Dark.css">
        <link rel="stylesheet" href="/assets/css/styles.min.css">
        <link rel="apple-touch-icon" sizes="180x180" href="/assets/img/apple-touch-icon.png">
        <link rel="icon" type="image/png" href="/assets/img/favicon-32x32.png" sizes="32x32">
    <link rel="icon" type="image/png" href="/assets/img/favicon-16x16.png" sizes="16x16">
    <script src='https://www.google.com/recaptcha/api.js'></script>
    <script src="https://cdn.ckeditor.com/4.9.2/full/ckeditor.js"></script>
</head>

And this is generate.php

    <?php require_once($_SERVER['DOCUMENT_ROOT'].'/core/confing.php');
    $page = (isset($_GET['page'])?(int)$_GET['page']:0);
    $story = (isset($_GET['story'])?(int)$_GET['story']:0);
    $action = "page".$page;
    $active = " active";
    $stmt = $db->prepare("SELECT * FROM content WHERE `id` = $story");
    $stmt->execute();
    while ($results = $stmt->fetch(PDO::FETCH_ASSOC)){
         if($story <= 11){
            echo $results["$action"];
                      }
                  }
//##############_TITLE_#####################
    if($story == 1){
      $title = "SA | Isama - stránka $page";
    }if($story == 2){
      $title = "SA | Allwin- stránka $page";
    }if($story == 3){
      $title = "SA | Samuel- stránka $page";
    }if($story == 4){
      $title = "SA | Isama - stránka $page";
    }if($story == 5){
      $title = "SA | IsamaNw - stránka $page";
    }if($story == 6){
      $title = "SA | IsamaNh - stránka $page";
    }if($story == 7){
      $title = "SA | Angel & Eklips - stránka $page";
    }if($story == 8){
      $title = "SA | Mr. Y  - stránka $page";
    }if($story == 9){
      $title = "SA | White Star - stránka $page";
    }if($story == 10){
      $title = "SA | Lord Terror - stránka $page";
    }if($story == 11){
      $title = "SA | Hyperion - stránka $page";
    }if(empty($story) || empty($page)) {
      $title = "SA | Nesprávný vstup";
    }
?>

It works when I try var_dump($title); inside index.php but not inside head.php ...

Upvotes: 0

Views: 1356

Answers (1)

confetti
confetti

Reputation: 1100

Move the includes' order around.

include(DIR . "/includes/generate.php"); // title is from here.
include(DIR . "/includes/menu.php");
include(DIR . "/includes/head.php");    // Styles
include(DIR . "/includes/footer.php");

If you are first including the code from head.php and trying to access $title there even though it gets assigned only in generate.php which is included later, $title won't be accessible by head.php.

Upvotes: 1

Related Questions