YouTip LogoYouTip

Php Includes

PHP include and require

PHP include and require

-

PHP Tutorial

PHP Forms

PHP Advanced

PHP 7 New Features

PHP Database

PHP XML

PHP and AJAX

PHP Reference

PHP date() Function

PHP Files

Deep Dive

Computer Science

Network Services

Programming

Scripting Languages

Web Service

Scripting

Programming Languages

Web Design and Development

Software

Development Tools

PHP Include Files


PHP include and require Statements

In PHP, you can insert the content of one file into another file before the server executes it.

The include and require statements are used to insert useful code written in other files into the execution flow.

include and require are identical in every way except for how they handle errors:

  • require produces a fatal error (E_COMPILE_ERROR) and stops script execution when an error occurs.
  • include produces a warning (E_WARNING) and continues script execution when an error occurs.

Therefore, if you want the execution to continue and output results to the user even if the include file is missing, use include. Otherwise, in frameworks, CMS, or complex PHP application programming, always use require to include critical files in the execution flow. This helps improve application security and integrity in case a critical file is accidentally missing.

Include files save a lot of work. This means you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs updating, you only need to update the header include file.

Syntax

include 'filename';

or

require 'filename';

PHP include and require Statements

Basic Example

Assume you have a standard header file named "header.php". To include this header file in a page, use include/require:

<html>
<head>
<meta charset="utf-8">
<title>(.com)</title>
</head>
<body>

<?php include 'header.php'; ?>
<h1>Welcome to my homepage!</h1>
<p>Some text.</p>

</body>
</html>

Example 2

Assume we have a standard menu file used on all pages.

"menu.php":

<?php
echo '<a href="/">Home</a><a href="/html">HTML Tutorial</a><a href="/php">PHP Tutorial</a>';
?>

All pages in the website should include this menu file. Here's how:

<html>
<head>
<meta charset="utf-8">
<title>(.com)</title>
</head>
<body>

<div class="leftmenu">
<?php include 'menu.php'; ?>
</div>
<h1>Welcome to my homepage!</h1>
<p>Some text.</p>

</body>
</html>

Example 3

Assume we have an include file that defines variables ("vars.php"):

<?php
$color='red';
$car='BMW';
?>

These variables can be used in the calling file:

<html>
<head>
<meta charset="utf-8">
<title>(.com)</title>
</head>
<body>

<h1>Welcome to my homepage!</h1>
<?php
include 'vars.php';
echo "I have a $color $car"; // I have a red BMW
?>

</body>
</html>

PHP date() Function

PHP Files

iFlytek Starry Coding Plan includes free model call quotas, DeepSeek, GLM, Kimi, MiniMax, one-stop experience and deployment platform. Configuration Guide Β₯3.9/month Subscribe Now

1 Note(s)

Write notes

1. #0 1966vng

zy1***vng@163.com

Reference address 126

Difference between include and require

  • require is generally placed at the very beginning of PHP files; the program imports the referenced file before execution;
  • include is generally placed within the program's flow control; it's only referenced when the program executes, simplifying the execution flow.
  • When a file included with require has an error, execution is interrupted and a fatal error is returned;
← Ng Ng RepeatNg Ng Repeat β†’