未定义的变量$ title-PHP

时间:2018-07-24 18:34:02

标签: php

我有index.php文件,我正在尝试像这样在head.php中回显$ title。

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

由于某种原因PHP返回了注意:未定义的变量: 7
NULL上的 /includes/head.php 中的标题。

所以我的第一个想法是尝试var_dump($ title);在index.php里面,确实我得到了标题,但是我写了var_dump($ title);到head.php及其NULL,我不知道为什么。我长期使用此解决方案,但从未遇到过此问题。 [是的,我尝试将标题写到index.php还是没有运气]。
   这是我的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");

编辑:这是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>

这是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";
    }
?>

当我尝试var_dump($ title)时,它可以工作;在index.php内,但不在head.php内...

1 个答案:

答案 0 :(得分:1)

移动包含”的顺序。

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");

如果您首先包含来自head.php的代码并尝试访问$title,即使该代码仅在稍后包含的generate.php中分配,则$title将无法被访问head.php。