是这样的,我想要在php里 include 另一个 充满html的模板php文件,但是报错变量找不到,
代码:

a.php

  1. <?php
  2. $one_2 = get_one();
  3.  
  4. function include_page($page)
  5. {
  6. include APP_PATH . ‘page/’ . $page;
  7. }
  8.  
  9.  
  10. //include ‘detail_page.php’;
  11. //include APP_PATH . ‘/page/article/detail_page.php’;
  12.  
  13. include_page(‘article/detail_page.php’);

article/detail_page

  1. <!doctype html>
  2. <html lang=“en”>
  3. <body>
  4. <?= $one_2[‘title’] ?>
  5. </body>
  6. </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 就好了