php include 找不到变量
是这样的,我想要在php里 include 另一个 充满html的模板php文件,但是报错变量找不到,
代码:
a.php
- <?php
- $one_2 = get_one();
- function include_page($page)
- {
- include APP_PATH . ‘page/’ . $page;
- }
- //include ‘detail_page.php’;
- //include APP_PATH . ‘/page/article/detail_page.php’;
- include_page(‘article/detail_page.php’);
article/detail_page
- <!doctype html>
- <html lang=“en”>
- <body>
- <?= $one_2[‘title’] ?>
- </body>
- </html>
结果报错:
Notice: Undefined variable: one_2 in D:\code\fast_php\page\article\detail_page.php on line 12
查阅资料: https://stackoverflow.com/questions/30969215/php-include-file-variables-losing-scope
被包含脚本可以使用的变量是 include 那行代码可以访问的变量。
所以这里的问题是 我把 include 写在了函数里,这样就访问不到全局变量 了。直接 在文件里 include 就好了