【问题标题】:Include php/html code or use function instead?包含 php/html 代码还是使用函数?
【发布时间】:2017-08-15 01:25:52
【问题描述】:

我试图找出在某些页面上包含一些 html/php 代码的最佳方式。例如页眉/页脚和其他一些重复的部分。

我应该 include_once(php 文件)(可能在一个页面中多次使用)还是使用一个函数并从那里获取代码?

最好的方法是什么?

【问题讨论】:

  • 你不需要创建一个函数来包含代码你可以使用 build in php function include 'header.php';

标签: php html function include


【解决方案1】:

使用函数:require 'file.php'require_once 'file.php'include 'file.php'、include_once 'file.php' 直接在你的代码中 html 或 php.... 使用示例:

<html>
<body>

<div class="menu">
<?php include 'menu.php';?>
</div>

<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>

</body>
</html>

当应用程序需要文件时使用 require。

当不需要文件并且应用程序应该使用包含时 找不到文件时继续。

使用 _once 仅加载文件一次

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 2012-02-05
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多