The PHP file is called with several functions include_once () require_once () include () Require (_php tutorial

Source: Internet
Author: User
Tags pear
Require ()
The Require () statement includes and runs the specified file.

The Require () statement includes and runs the specified file. For more information about how to work, see the documentation for include ().

Require () and include () are exactly the same in all respects except how to deal with failures. The include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing a page when you lose a file, don't hesitate to use require (). The include () is not the case and the script will continue to run. Also be sure to set the appropriate include_path.

Example 16-2. Basic require () example

Require ' prepend.php ';

Require $somefile;

Require (' somefile.txt ');

?>


See the Include () documentation for more examples.


Note: The following rules apply before PHP 4.0.2: require () always attempts to read the target file, even if the row it is in is not executed at all. Conditional statements do not affect require (). However, if the row where require () is not executed, the code in the destination file will not execute. Similarly, the cyclic structure does not affect the behavior of require (). Although the code contained in the destination file is still the body of the loop, require () itself runs only once.


Note: Because this is a language structure and not a function, it cannot be called by a "variable function".

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

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

When a file is included, the code contained in it inherits the scope of the variable for the row in which the include is located. From there, any variables that are available to the calling file at that line are also available in the called file.

Example 16-3. Basic include () example

vars.php

$color = ' green ';
$fruit = ' Apple ';

?>

test.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 range of the function.

Example 16-4. Included in the function

function foo ()
{
Global $color;

Include ' vars.php ';

echo "a $color $fruit";
}

/* vars.php is in the scope of foo () so *
* $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 goes out of PHP mode at the beginning of the target file and enters HTML mode and resumes at the end of the file. For this reason, any code in the target file that should be executed as PHP code must be included in the valid PHP start and end tags.

If URL fopen wrappers is activated in PHP (the default configuration), you can specify which files to include by URL (see appendix L for HTTP or other supported encapsulation protocols-supported protocols) instead of local files. If the destination server interprets the destination file as PHP code, you can pass the variable to the included file with the URL request string for the HTTP GET. Strictly speaking, this is not the same as including a file and inheriting the variable space of the parent file; The script file is actually already running on the remote server, and the local script includes its results.


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

Example 16-5. Include () via HTTP

/* This example assumes, Www.zhutiai.com is configured to parse. PHP *
* files and not. txt files. Also, ' works ' here means, the variables *
* $foo and $bar is 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.

?>


See Using remote files, fopen () and file () for related information.

Because include () and require () are special language constructs, they must be placed in a statement group (in curly braces) in a conditional statement.

Example 16-6. Include () and conditional statement groups

This is wrong and would not be work as desired.
if ($condition)
Include $file;
Else
Include $other;


This is correct.
if ($condition) {
Include $file;
} else {
Include $other;
}

?>


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


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


Example 16-7. Include () and return () statements

return.php

$var = ' php ';

return $var;

?>

noreturn.php

$var = ' php ';

?>

testreturns.php

$foo = include ' return.php ';

Echo $foo; Prints ' php '

$bar = include ' noreturn.php ';

Echo $bar; Prints 1

?>


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

Note: Because this is a language structure and not a function, it cannot be called by a "variable function".
Require_once ()
The require_once () statement includes and runs the specified file during script execution. This behavior is similar to the Require () statement, except that if the code in the file is already included, it will not be included again. See the documentation for require () for how this statement works.

Require_once () should be used in cases where the same file may be included more than once during script execution, you want to make sure that it is included only once to avoid problems such as function redefinition, variable re-assignment, and so on.

See the Pear code in the latest PHP source release package using require_once () and include_once () examples.


Note: require_once () is a new addition to PHP 4.0.1pl2.

Note: Be aware that the behavior of require_once () and include_once () in case-insensitive operating systems (such as Windows) may not be what you expect. Example 16-8. Require_once () is case insensitive under Windows

require_once ("a.php");//This would include a.php
Require_once ("a.php"); This would include a.php again on windows!
?>



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

Include_once ()
The include_once () statement includes and runs the specified file during script execution. This behavior is similar to the include () statement, except that if the code in the file is already included, it will not be included again. As the name of this statement implies, it is included only once.

Include_once () should be used in cases where the same file may be included more than once during script execution, you want to make sure that it is included only once to avoid problems such as function redefinition, variable re-assignment, and so on.

See the Pear code in the latest PHP source release package for more examples using require_once () and include_once ().


Note: include_once () is a new addition to PHP 4.0.1pl2.

Note: Be aware that the behavior of include_once () and require_once () in case-insensitive operating systems (such as Windows) may not be what you expect. Example 16-9. Include_once () is case insensitive under Windows

include_once ("a.php");//This would include a.php
Include_once ("a.php"); This would include a.php again on windows!
?>


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

http://www.bkjia.com/PHPjc/632301.html www.bkjia.com true http://www.bkjia.com/PHPjc/632301.html techarticle The require () require () statement includes and runs the specified file. The Require () statement includes and runs the specified file. For more information about how to work, see the documentation for include (). Require () and ...

  • Related Article

    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.