阅读量:113
include_once 是 PHP 语言中的一个内置函数,用于在当前脚本中包含指定的文件仅一次
下面是一个简单的示例来说明如何使用 include_once:
- 创建一个名为
header.php的文件,其中包含一些通用的 HTML 头部信息:
<!DOCTYPE html>
<html lang=>
<head>
<meta charset=>
<meta name= content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
- 创建一个名为
content.php的文件,其中包含一些页面内容:
Welcome to my website!
This is some content.
- 在主脚本文件(例如
index.php)中使用include_once来包含header.php和content.php文件:
<?php
include_once 'header.php';
include_once 'content.php';
?>
</body>
</html>
在这个例子中,header.php 和 content.php 文件只会被包含一次,即使 index.php 脚本被多次调用。这有助于避免因多次包含相同文件而导致的潜在问题,例如函数重定义或变量重复声明。