PHP include and require
-
Computer Science Network Services Programming Scripting Languages Web Service Scripting Programming Languages Web Design and Development Software Development Tools 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: 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. or Assume you have a standard header file named "header.php". To include this header file in a page, use include/require: Assume we have a standard menu file used on all pages. "menu.php": All pages in the website should include this menu file. Here's how: Assume we have an include file that defines variables ("vars.php"): These variables can be used in the calling file: Write notes 1. #0 1966vng zy1***vng@163.com Difference between include and requirePHP Tutorial
PHP Forms
PHP Advanced
PHP 7 New Features
PHP Database
PHP XML
PHP and AJAX
PHP Reference
Deep Dive
PHP Include Files
PHP include and require Statements
Syntax
include 'filename';
require 'filename';
PHP include and require Statements
Basic Example
<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
<?php
echo '<a href="/">Home</a><a href="/html">HTML Tutorial</a><a href="/php">PHP Tutorial</a>';
?><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
<?php
$color='red';
$car='BMW';
?><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>1 Note(s)
YouTip