Home Backend Development PHP Tutorial An in-depth analysis of the difference between php include and require_PHP Tutorial

An in-depth analysis of the difference between php include and require_PHP Tutorial

Jul 21, 2016 pm 03:05 PM
include php require the the difference and go deep of parse statement

nclude()
The include() statement includes and runs the specified file.

The following documentation also applies to require(). The two structures are identical except for how they handle failure. include() produces a warning and require() causes a fatal error. In other words, use require() if you want to stop processing the page if a missing file is encountered. This is not the case with include() and the script will continue to run. Also make sure the appropriate include_path is set.

When a file is included, the code contained in it inherits the variable scope of the include line. From that point on, any variables available in the calling file at that line are also available in the called file.

Example 12-3. Basic include() example
vars.php

Copy code The code is as follows:

$color = 'green';
$fruit = 'apple';
?>
echo "A $color $fruit"; // A
include 'vars.php';echo "A $color $fruit"; // A green apple
?>
If include appears in a function in a calling file, all code contained in the called file will behave as if it were defined inside that function. So it will follow the variable scope of that function.
Example 12-4. Includes in functions





Copy code
The code is as follows:


function foo(){
global $color;
include 'vars.php';
echo "A $color $ fruit";
}/* vars.php is in the scope of foo() so ** $fruit is NOT available outside of this ** scope. $color is because we declared it ** as global. */
foo(); // A green apple
echo "A $color $fruit"; // A green
?>


When a file is included, the parser leaves PHP mode and enters HTML mode at the beginning of the target file, and resumes at the end of the file. For this reason, any code in an object file that should be executed as PHP code must be included in valid PHP start and end tags.

If "URL fopen wrappers" are activated in PHP (default configuration), files to be included can be specified using URLs (via HTTP) instead of local files. If the target server interprets the target file as PHP code, you can pass variables to the included file using the URL request string for HTTP GET. Strictly speaking, this is not the same thing as including a file and inheriting the variable space of the parent file; the script file has actually been run on the remote server, and the local script includes its results.



Warning


The Windows version of PHP does not currently support remote file access for this function, even if the allow_url_fopen option is activated.


Example 12-5. include() over HTTP



Copy code
The code is as follows:


/* This example assumes that www.example.com is configured to parse .php ** files and not .txt files. Also, 'Works' here means that the variables *
* $foo and $bar are available within the included file. */
// Won't work; file.txt wasn't handled by www.example.com as PHPinclude 'http://www.example.com/file.txt?foo=1&bar=2';
// Won't work; looks for a file named 'file.php ?foo=1&bar=2' on the// local filesystem.include 'file.php?foo=1&bar=2';
// Works.
include 'http: //www.example.com/file.php?foo=1&bar=2';

$foo = 1;
$bar = 2;
include 'file.txt'; // Works.
include 'file.php'; // Works.
?>


For related information, see Using remote files, fopen() and file().
Because include() and require() are special language constructs, they must be placed in a statement group (in curly braces) when used in conditional statements.



Example 12-6. include() and conditional statement group





Copy code
The code is as follows:


// This is WRONG and will not work as desired.if ($condition)
include $file;
else include $other;
// This is CORRECT.if ($condition) {include $file;} else {include $other;
}
?>


Handling return values: You can use the return() statement in an included file to terminate the execution of the program in the file and return to the script that called it. It is also possible to return values ​​from included files. The return value of an include call can be obtained like a normal function.

Note: In PHP 3, return cannot appear in included files unless called in a function. In this case return() acts on the function rather than the entire file.

Example 12-7. include() and return() statements
return.php
Copy Code The code is as follows:

$var = 'PHP';
return $var;
?>

noreturn.php
Copy code The code is as follows:

$var = 'PHP';
?>

testreturns.php
Copy code The code is as follows:

$foo = include 'return.php';
echo $foo; // prints 'PHP'
$ bar = include 'noreturn.php';
echo $bar; // prints 1
?>

The value of $bar is 1 because include ran successfully. Note the difference in the above examples. The first uses return() in the included file and the other does not. Several other ways to "include" files into variables are to use fopen(), file() or include() in conjunction with output control functions.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327653.htmlTechArticlenclude() The include() statement includes and runs the specified file. The following documentation also applies to require(). The two structures are identical except for how they handle failure. include() generates an alert...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

The difference between laravel and thinkphp The difference between laravel and thinkphp Apr 18, 2025 pm 01:09 PM

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

See all articles