簡體   English   中英

PHP包含一個包含所包含文件的文件->腳本和變量被包含在內,但似乎回顯了

[英]PHP include a file with an included file -> scripts and variables get included but seem echo'd

我在包含一個包含包含某些js腳本庫的文件的文件時遇到了麻煩。

index.php

<?php
include 'header.php';
?>
<section>
  <div>
     <p>misc. html</p>
  </div>
</section>
<?php
include ($_SERVER['DOCUMENT_ROOT'].'/footer.php');
echo 'test ='.$testVar;
?>

footer.php

<?php
include ($_SERVER['DOCUMENT_ROOT'].'/includes/footerscripts.php');
?>
</body>
</html>

/includes/footerscripts.php

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<?php
$testVar = 'test';
?>

在這種情況下,索引文件在運行時的源代碼中確實包含以下內容:

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
test =

腳本正在那里,但是沒有被執行,並且變量都沒有通過。

當我將footer.php更改為直接包含那些腳本和變量時,該變量將其更改為index.php,腳本也這樣做了,但它們沒有執行(或未按時執行)。

有想法嗎?

我有一個奇怪的php5.ini文件,因為我不知道要放什么,如果有什么不同的話。 包含文件路徑似乎還可以,因為它在大多數情況下仍然有效。

謝謝,馬特

編輯:添加了include('header.php'),它可以正常工作並提取我的標題。

該文件看起來像:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="--">
<meta name="keywords" content="--">
<meta name="author" content="--">
<title>--</title>
<link rel="stylesheet" href="/css/mainstylesheet.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="icon" href="/images/favicon.ico">
</head>
<body>
    <header class="nav-top" id="top" role="navigation">
        <div class="container">
            <div class="home-link">
                <a href="../"><img src="/images/logo/logo-p.png" alt="plc" class="logo" /></a>
            </div>
            <nav class="nav-main">
                <ul>
                    <li>
                        <a href="../">Home</a>
                    </li>
                    <li>
                        <a href="/portfolio">Portfolio</a>
                    </li>
                    <li>
                        <a href="/contact">Contact</a>
                    </li>
                </ul>
            </nav>
        </div>
    </header>

編輯:猜猜我只是需要從文檔的頭加載我的jquery,因為它可以那樣工作。 我仍然無法將孫子變量傳遞給父母,但是哦

根據您的示例, index.php文件缺少某些內容:

<?php
include ($_SERVER['ROOT'].'/footer.php');
echo 'test ='.$testVar;
?>

我假設您沒有發布整個文件,因為此處的輸出hmtl無效-缺少html開頭標記等。您能否提供整個和/或簡化的索引文件?

$_SERVER['ROOT']所有實例替換為$_SERVER['DOCUMENT_ROOT']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM