PHP include and require statements
In PHP, you can insert a The contents of the file.
include and require statements are used to insert useful code written in other files into the execution flow.
include and require are identical except for how they handle errors:
require generates a fatal error (E_COMPILE_ERROR) after which the script stops executing.
include generates a warning (E_WARNING) and the script will continue execution after the error occurs.
So if you want to continue execution and output the 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 'file name'; can also be written as include('file name');
or
require 'file name'; It can also be written as require('file name');
Example: You need to create two files yourself and put them in Test on the local server
Now let’s create two new files. The head.php file code is as follows:
<?php echo 123; ?>
Then We want to create another file main.php. The code is as follows
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网</title> </head> <body> <?php //include('head.php'); //使用include 包含 //require('head.php'); //使用 require 包含 ?> </body> </html>
When we run the main.php file, 123 will be output. If the file name we include is wrong, we will be prompted that the file cannot be found
Next Section