我正在学习Php
我制作了一个主要的index.php页面在某个时刻,它包含行
<?php
include './BaseTemplate.php';
?>
BaseTemplate.php包含许多常见的普通html,这些html在所有页面之间都是相等的。它看起来像(但这只是几行):
echo '<script src = "../assets/js/intention.js"></script>';
echo '<script src = "../assets/js/context.js"></script>';
echo '<head><body>';
echo '<table>';
但是这些echo命令没有执行,我应该如何解决这个问题?
当您将任何文件包含到页面中时,该文件的代码将进入页面。因此,如果包含的文件和包含它的页面不在同一路径中,就会产生问题。
你似乎只得到了那个。因此,请根据index.php在BaseTemplate.php中创建路径,它就会工作。
我解决了这个问题。我想include会把所有代码从外部php源文件放在一个页面中。事实并非如此,include文件是具有可调用函数的库。
为了解决这个问题,我把它重写为BaseTemplate.php,就像这个一样
<?php
Function WritePageBase() { // now i put it all inside a callable function
echo '<script src = "../assets/js/intention.js"></script>';
echo '<script src = "../assets/js/context.js"></script>';
echo '<head><body>';
echo '<table>';
}
?>
现在我的其他页面可以称之为
<?php
include './BaseTemplate.php'; // include the file with functions
WritePageBase(); // call the function to echo all html code in page
?>
这减少了许多html代码,而这些代码现在可以由一个文件编辑(使用它的所有其他页面也会更改)。