首页 \ 问答 \ PHP需要/包含文件(PHP require/include file)

PHP需要/包含文件(PHP require/include file)

我有以下程序结构:

Root directory:-----index.php
                       |-------TPL directory
                                 |-------------header.php
                                 |-------------index.php
                                 |-------------footer.php
php file loading structure:

Root directory:index.php ----require/include--->tpl:index.php
                                                      |----require/include-->header.php
                                                      |----require/include-->footer.php

root index.php:

    <?php
function get_header(){
    require('./tpl/header.php');
}

function get_footer(){
    require('./tpl/footer.php');
}

$a = "I am a variable";

要求( './ TPL / index.php的');

TPL:index.php文件:

    <?php
get_header();//when I change require('./tpl/header.php'); variable $a can work!!
echo '<br />';
echo 'I am tpl index.php';
echo $a;
echo '<br />';
get_footer();//when I change require('./tpl/footer.php'); variable $a can work!!

TPL:header.php文件:

    <?php
echo 'I am header.php';
echo $a;//can not echo $a variable
echo '<br/>';

TPL:footer.php:

    <?php
echo '<br />';
echo $a;//can not echo $a variable
echo 'I am footer';

出于某种原因,当我使用函数来要求header.php和footer.php时,我的变量$ a没有得到回应。 如果我自己使用header.php或footer.php,它工作正常。 我不明白问题是什么。 您认为这里的问题是什么?


I have the following program Structure:

Root directory:-----index.php
                       |-------TPL directory
                                 |-------------header.php
                                 |-------------index.php
                                 |-------------footer.php
php file loading structure:

Root directory:index.php ----require/include--->tpl:index.php
                                                      |----require/include-->header.php
                                                      |----require/include-->footer.php

Root index.php :

    <?php
function get_header(){
    require('./tpl/header.php');
}

function get_footer(){
    require('./tpl/footer.php');
}

$a = "I am a variable";

require('./tpl/index.php');

TPL:index.php:

    <?php
get_header();//when I change require('./tpl/header.php'); variable $a can work!!
echo '<br />';
echo 'I am tpl index.php';
echo $a;
echo '<br />';
get_footer();//when I change require('./tpl/footer.php'); variable $a can work!!

TPL:header.php:

    <?php
echo 'I am header.php';
echo $a;//can not echo $a variable
echo '<br/>';

TPL:footer.php:

    <?php
echo '<br />';
echo $a;//can not echo $a variable
echo 'I am footer';

For some reason when I used function to require header.php and footer.php my variable $a doesn't get echoed. It works fine if I use header.php or footer.php on its own. I do not understand what the problem is. What do you think is the issue here?


原文:https://stackoverflow.com/questions/16883839
更新时间:2023-12-29 20:12

最满意答案

如“ 如何使用滚动窗格”中所示 ,您可以使用组件的scrollRectToVisible()方法滚动到任意Rectangle 。 这里有一个例子。

附录:作为一个Container ,即使JPanel具有相当多的嵌套内容,它也是相当可替代的。 在给定级别交换内容的一种可靠方法是通过CardLayout此处此处显示


I solved the problem by using the JScrollbar method setValues(), which allows me to set at the same time the maximum, minimum, value and extent of the scrollbar. By setting the maximum and minimum to the values I want, the scrollbar behaves as I wanted/expected.

The problem was that I was only setting maximum and minimum values (setMaximum, setMinimum), and since there is a strict policy at the model that minimum <= value <= value+extent <= maximum, that estrategy did not work.

相关问答

更多
  • 截至目前,最好的方法是在xml中使用android:fadeScrollbars="false" ,相当于ScrollView.setScrollbarFadingEnabled(false); 在java代码中。 As of now the best way is to use android:fadeScrollbars="false" in xml which is equivalent to ScrollView.setScrollbarFadingEnabled(false); in java c ...
  • 你可以,但它不会是完美的。 复制此帖子中的代码: // Used like $('#my-id').hasScrollbar(); jQuery.fn.hasScrollbar = function() { var scrollHeight = this.get(0).scrollHeight; //safari's scrollHeight includes padding if ($.browser.safari) scrollHeight -= parseInt(this.css('pad ...
  • 添加overflow:auto; 对于css中的类.wrap并删除style="overflow-x:scroll;overflow-y:auto;" 来自html中的article标签。 我希望这有帮助。 Add overflow:auto; for the class .wrap in css and remove style="overflow-x:scroll;overflow-y:auto;" from the article tag in html. I hope this helps.
  • 如“ 如何使用滚动窗格”中所示 ,您可以使用组件的scrollRectToVisible()方法滚动到任意Rectangle 。 这里有一个例子。 附录:作为一个Container ,即使JPanel具有相当多的嵌套内容,它也是相当可替代的。 在给定级别交换内容的一种可靠方法是通过CardLayout , 此处和此处显示 。 I solved the problem by using the JScrollbar method setValues(), which allows me to set at t ...
  • 评论后更新如果主体的宽度不超过浏览器的视口: 在元素内滚动 : var elem = document.getElementById("container"); //div#container var elemWidth = elem.scrollWidth; var elemVisibleWidth = elem.offsetWidth; elem.scrollLeft = (elemWidth - elemVisibleWidth) / 2; 使用此代码垂直和水平居中页面(另请参阅: 本答案 ): ...
  • 您可以使用以下css代码调整iframe或任何元素的滚动条: iframe{ width:500px; height: 500px; overflow-x: hidden; overflow-y: scroll } I actually managed to resolve this issue myself using this approach with jQuery. $('testIframe').on('load', function(){ resizeIframe(this); ...
  • 我用mcustomscrollbar实现了我想要的结果。 但是我花了一段时间才弄清楚如何在一个反应项目中使用它,我在这里解释了: Reactjs:带有reactjs的jQuery自定义内容滚动条 I achieved the results I want with mcustomscrollbar. But it took me a while to figure out how to use it within a react project, which I explained here: Reactj ...
  • 我知道这是一个老问题,但是你的脚本帮我处理了固定的div。 在我的情况下,我有100%宽度固定标头。 一切正常,在启动脚本后,固定div中的一些元素仍然改变了位置。 所以,在你的脚本中我包含了这个: $('.header').css({ 'width': 'calc(100% - ' + margin+ 'px)' }); 关闭脚本后: $('.header').css('width','100%'); 正如我之前所说,在我的情况下,这工作得很好。 完整脚本如下所示: 点击: var width = $ ...
  • 终于找到了一个解决方案后,我的头撞墙再过几个小时:似乎问题是由于SharePoint仍然在加载其内容(来自'sp.js'),并且因为时髦的东西它设置滚动条,我的脚本驱动的更改必须在SP滚动条完全初始化之前应用。 所以这是必须做的事情: $(document).ready(function(){ ExecuteOrDelayUntilScriptLoaded(runAfterSharePointIsDoneBeingStupid, 'sp.js'); }); function runAfterSha ...
  • 滚动窗格的视图没有任何附加内容,因此没有任何内容可以滚动... 您需要使用scrollPane.setViewportView(...);等内容为滚动提供组件视图scrollPane.setViewportView(...); 您可能需要查看如何使用滚动窗格获取更多详细信息 代码中有很多东西让我担心...... GUI环境中的Thread.sleep总是令人担忧,因为这实际上可能会导致应用程序无响应。 出于同样的原因使用无限循环。 使用KeyListener只是执行ActionListener的相同功能 您 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)