博客列表 >require与include加载示例

require与include加载示例

若是梦终会醒
若是梦终会醒原创
2020年04月28日 16:46:18824浏览

1.demo.php文件内容

  1. <?php
  2. $porm = $_POST['load'] ?? null;
  3. echo $porm;
  4. $file = 'load.php';
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="zh-CN">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  11. <title>加载</title>
  12. <style>
  13. * {
  14. margin: 0;
  15. padding: 0;
  16. }
  17. div {
  18. width: 1000px;
  19. height: 900px;
  20. margin: 0 auto;
  21. }
  22. div>div {
  23. width: 100%;
  24. height: 33.3%;
  25. }
  26. .btn {
  27. position: fixed;
  28. top: 100px;
  29. left: 50px;
  30. font-size: 18px;
  31. }
  32. h3 {
  33. color: red;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div>
  39. <?
  40. switch (true):
  41. case $porm < 2:
  42. if (file_exists($file) && is_file($file) && $porm) : //条件加载的条件
  43. echo '<h3>条件存在加载</h3>';
  44. include "{$file}";
  45. else :
  46. echo '<h3>条件不存在加载</h3>';
  47. include 'default.php';
  48. endif;
  49. break;
  50. case $porm < 4:
  51. if ($porm == 2) :
  52. echo '<h3>不去重加载</h3>';
  53. include "{$file}";
  54. include "{$file}";
  55. include "{$file}";
  56. else :
  57. echo '<h3>去重加载</h3>';
  58. include_once "{$file}";
  59. include_once "{$file}";
  60. include_once "{$file}";
  61. endif;
  62. break;
  63. case $porm < 6:
  64. if ($porm == 4) :
  65. echo '<h3>强制加载</h3>';
  66. require "{$file}";
  67. require "{$file}";
  68. require "{$file}";
  69. else :
  70. echo '<h3>强制去重加载</h3>';
  71. require_once "{$file}";
  72. require_once "{$file}";
  73. require_once "{$file}";
  74. endif;
  75. break;
  76. case $porm < 8:
  77. if ($porm == 6) :
  78. echo '<h3>include和require加载区别之include加载</h3>';
  79. include "{$file}s";
  80. else :
  81. echo '<h3>include和require加载区别之require加载</h3>';
  82. require "{$files}s";
  83. endif;
  84. break;
  85. endswitch;
  86. ?>
  87. </div>
  88. <section class="btn">
  89. <form action="demo.php" method="post">
  90. <select name="load" id="" style='height: 50px; font-size: 16px; background: pink;'>
  91. <option value="0">条件不存在</option>
  92. <option value="1" <? echo $porm == 1 ? 'selected' : null; ?>>条件存在</option>
  93. <option value="2" <? echo $porm == 2 ? 'selected' : null; ?>>不去重加载</option>
  94. <option value="3" <? echo $porm == 3 ? 'selected' : null; ?>>去重加载</option>
  95. <option value="4" <? echo $porm == 4 ? 'selected' : null; ?>>强制加载</option>
  96. <option value="5" <? echo $porm == 5 ? 'selected' : null; ?>>强制去重加载</option>
  97. <option value="6" <? echo $porm == 6 ? 'selected' : null; ?>>include和require加载区别之include加载</option>
  98. <option value="7" <? echo $porm == 7 ? 'selected' : null; ?>>include和require加载区别之require加载</option>
  99. </select>
  100. <input type="submit" value="确定">
  101. </form>
  102. </section>
  103. </body>
  104. </html>

2.load.php文件内容

  1. <?php
  2. $str = <<< DIV
  3. <div class="ifLoad" style="background-color: pink;"></div>
  4. <div class="quzLoad" style="background-color: yellowgreen;"></div>
  5. DIV;
  6. echo $str;

3.default.php文件内容

  1. <?php
  2. $str = <<< 'DIV'
  3. <div class="quzLoad" style="background-color: yellowgreen;"></div>
  4. <div class="ifLoad" style="background-color: pink;"></div>
  5. DIV;
  6. echo $str;

演示效果图:
1.条件不存在加载

2.条件存在加载

3.不去重加载

4.去重加载

5.强制加载

6.强制去重加载

7.include和require加载区别之include加载

8.include和require加载区别之require加载

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议