The difference between PHP include and require deep parsing _php tips

Source: Internet
Author: User
Tags variable scope vars
Nclude ()
The include () statement includes and runs the specified file.

The following documents also apply to require (). The two structures are exactly the same except how to handle failures. Include () produces a warning and require () causes a fatal error. In other words, if you want to stop processing the page when you encounter a missing file, use require (). Include () is not the case, the script will continue to run. Also, be sure to set the appropriate include_path.

When a file is included, the code that it contains inherits the scope of the variable in the row containing the include. Starting at this point, any variables that the calling file is available at that line are also available in the called file.

example 12-3. Basic include () example
vars.php
Copy Code code as follows:

<?php
$color = ' green ';
$fruit = ' Apple ';
?>

test.php
Copy Code code as follows:

<?php
echo "A $color $fruit"; A
Include ' vars.php ';
echo "A $color $fruit"; A Green apple
?>

If include appears in a function in the calling file, all the code contained in the called file will behave as if they were defined inside the function. So it will follow the variable scope of the function.

Example 12-4 is included in the function
Copy Code code as follows:

<?php
function foo ()
{
Global $color;
Include ' vars.php ';
echo "A $color $fruit";
}
/* vars.php is in the scope of Foo ()
* $fruit isn't 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 moves out of the PHP mode at the beginning of the target file and into HTML mode to recover at the end of the file. For this reason, any code in the target file that should be executed as a PHP code must be included in a valid PHP start and end tag.

If the URL fopen wrappers is activated in PHP (the default configuration), you can specify which files to include by using a URL (via HTTP) instead of a local file. If the target server interprets the destination file as a PHP code, you can pass variables to the included file with a URL request string that applies to HTTP GET. Strictly speaking this is not the same as the variable space that includes a file and inherits the parent file; The script file is actually running on the remote server, and the local script includes its results.

Warning
Windows version of PHP does not currently support remote file access for this function, even though the Allow_url_fopen option has been activated.

example 12-5. Include () via HTTP
Copy Code code as follows:

<?php
/* This example assumes that www.example.com be configured to parse. PHP *
* files and not. txt files. Also, "works" here means the variables *
* $foo and $bar are available within the included file. */
Won ' t work; File.txt wasn ' t handled by www.example.com as PHP
Include ' 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 information, see Using remote Files, fopen (), and file ().
Because include () and require () are special language constructs, you must put them in a statement group (in curly braces) in a conditional statement.

Example 12-6. Include () and conditional statement groups
Copy Code code as follows:

<?php
This is wrong and would not work as desired.
if ($condition)
Include $file;
Else
Include $other;
This is correct.
if ($condition) {
Include $file;
} else {
Include $other;
}
?>

processing return values:You can use the return () statement in the included file to terminate the execution of the program in the file and back the script that called it. You can also return a value from a included file. You can get the return value of an include call like a normal function.

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

examples 12-7. Include () and return () statements
return.php
Copy Code code as follows:

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

noreturn.php
Copy Code code as follows:

<?php
$var = ' PHP ';
?>

testreturns.php
Copy Code code as follows:

<?php
$foo = include ' return.php ';
Echo $foo; Prints ' PHP '
$bar = include ' noreturn.php ';
Echo $bar; Prints 1
?>

The $bar value is 1 because the include was successfully run. Note the differences in the above examples. The first one is in the included file with return () and the other does not. Several other ways to "include" a file into a variable are used with fopen (), file (), or include () along with the output control function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.