PHP include files
PHP include and require statements
In PHP, you can execute it on the server PHP file before inserting the contents of a file into the file.
include and require statements are used to insert useful code written in other files into the execution flow.
Include and require are the same except for the way they handle errors:
· but the script will stop executing after the error occurs. .
· include generates a warning (E_WARNING), and the script will continue to execute after the error occurs.
So if you want to continue execution and output results to the user even if the included file is missing, then use include. Otherwise, in frameworks, CMS, or complex PHP application programming, always use require to reference key files to the execution flow. This helps improve application security and integrity in the event that a critical file is accidentally lost.
Including files saves a lot of work. This means you can create standard header, footer or menu files for all web pages. Then, when the header needs updating, you simply update the header include file.
Syntax
include 'filename';
or
require 'filename';
PHP include and require statements
Basic example
Suppose you have a standard page header file named "header. php". To reference this header file in the page, please use include/require:
<html> <head> <meta charset="utf-8"> <title> php中文网 (php.cn)</title> </head> <body> <?php include 'header.php'; ?> <h1>欢迎来到我的主页!</h1> <p>一些文本。</p> </body> </html>
Example 2
Suppose we have a standard menu file used in all pages.
"menu.php":
echo '<a href="/">Homepage</a>
<a href="/html ">HTML Tutorial</a>
<a href="/php">PHP Tutorial</a>';
All pages in the website should be referenced The menu file. The following is the specific approach:
<html> <head> <meta charset="utf-8"> <title> php中文网 (php.cn)</title> </head> <body> <div class="leftmenu"> <?php include 'menu.php'; ?> </div> <h1>欢迎来到我的主页!</h1> <p>一些文本。</p> </body> </html>
Example 3
Suppose we have an include file ("vars.php") that defines variables:
<?php $color='red'; $car='BMW'; ?>
These variables can be used in calls In the file:
<html> <head> <meta charset="utf-8"> <title>php中文网 (php.cn)</title> </head> <body> <h1>欢迎来到我的主页!</h1> <?php include 'vars.php'; echo "I have a $color $car"; // I have a red BMW ?> </body> </html>
include file that does not exist
include "foo.php";
The above code contains a file called foo.php file. If the file does not exist, the following warning message will appear after execution.
Warning: include(foo.php): failed to open stream: No such file or directory in /var/www/web/ test.php on line 3
Warning: include(): Failed opening 'foo.php' for inclusion (include_path='.:/usr/local/php/lib/php') in /var/www /web/test.php on line 3
Set absolute path
include 'C:\wamp\www \foo.php';
The absolute path is set above, successfully avoiding include_path retrieval, but what if the code is COPYed to others, and they use D:\wamp or E:\www?
If the current script is C:\wamp\www\index.php, then index.php can be changed to
include __DIR__ . '\foo.php' ;
Still the previous exercise, change the relative path to an absolute path
once
include and require respectively correspond to a once function:
include_once and require_once
There is no difference in the functions of the functions. The main function is to avoid repeated inclusion.
Separation of templates
Since Use include or require to include other code, then we can also separate the code into different files
<?php
$name = 'andy';
$age = 18;
?>
<h1><?=$name?> is <span><?=$age?></span> years old</h1>
<?php
$name = 'andy';
$age = 18;
require "template.html";
return
Inclusion can also play new tricks. If it is a PHP file, you can use the return statement in the included file
<?php ;?php
$arr = include "return.php"; ?>
PHP include_path
When the functions include(),require(),fopen_with_path( ) function to search for files. Without setting include_path, these functions will search in the web root directory by default when opening files. When include_path is set, these php functions will first search in the specified include_path directory. 1.The meaning of include_pathWhen the functions include(), require(), and fopen_with_path() are used to search for files. When include_path is not set, when these functions open files, they will search in the web root directory by default. When include_path is set, , these php functions will first search under the specified include_path directory.
The principle is similar to the environment variables of the window system. When the window runs the cmd command, after entering some cmd commands, the system will set the Search the environment variables to see if these commands exist. If they exist, they can be executed.
2.include_path settings
The first method:
Modify the include_path item in the php.ini file.
include_path = .:/usr/local/lib/php:./include
Second method:
Use the ini_set method.
ini_set("include_path", ".:../:./include:../include");
3. Note
zendframework include setting index.php
Copy code The code is as follows:
set_include_path('.' .PATH_SEPARATOR.'../library/'
.PATH_SEPARATOR.'./application/models/'
.PATH_SEPARATOR.'./ application/lib/'
.PATH_SEPARATOR.get_include_path());
PATH_SEPARATOR is a constant, which is a ":" number in Linux systems and a ";" number on Windows.
Therefore, it is best to use the constant PATH_SEPARATOR instead when writing a program, otherwise errors will occur if the system is transplanted from linux to win system or vice versa!
get_include_path gets the current existing environment variables, plus the previous settings, it is the new system include.