PHP

PHP 文件加载的四种方式

2020-04-26  本文已影响0人  887d1fc86fe6

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <?php
    $file = __DIR__ . '/lib/nav.html';
    // 避免重复导入,只会生效一次
    // include_once 载入失败可以继续运行下面后续代码。
    include_once $file;
    include_once $file;
    // 避免重复导入,只会生效一次
    // require_once 载入失败则报错后终止程序。
    require_once $file;
    require_once $file;
  ?>
</body>
</html>




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <?php
    echo '__DIR__ 当前路径为:', __DIR__;
    $file = __DIR__ . '/lib/nav.html';
    echo '<br>__DIR__ 想要载入路径为:', $file;
    include $file; // 这样也是能生效的

    echo '<hr>';

    echo 'getcwd() 当前路径为:', getcwd();
    $file1 = getcwd() . '/lib/nav.html';
    echo '<br>getcwd() 想要载入路径为:', $file1;
    include $file1; // 这样也是能生效的

    echo '<hr>';

    echo '__DIR__ 与 getcwd() 区别:';
    include 'lib/page.php';
  ?>
</body>
</html>
<?php
  echo '<br>__DIR__ 获取当前文件路径:';
  echo '<br>' . __DIR__;
  echo '<br>getcwd() 获取当前浏览网页的路径,也就是你当前浏览访问的页面在的路径,而不是当前文件的路径:';
  echo '<br>' . getcwd();
?>
上一篇 下一篇

猜你喜欢

热点阅读