Home  >  Article  >  Backend Development  >  PHP file calls and contains several functions include_once() require_once()include()require(_PHP tutorial

PHP file calls and contains several functions include_once() require_once()include()require(_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:54:381019browse

require()
The require() statement includes and runs the specified file.

The

require() statement includes and runs the specified file. See the documentation for include() for details on how includes work.

require() and include() are identical in every way except how they handle failure. include() produces a warning and require() results in a fatal error. In other words, if you want to stop processing the page if a file is missing, don't hesitate to use require(). This is not the case with include() and the script will continue to run. Also make sure the appropriate include_path is set.

Example 16-2. Basic require() example

require 'prepend.php';

require $somefile;

require ('somefile.txt');

?>


See the include() documentation for more examples.


Note: Before PHP 4.0.2 the following rule applied: require() would always try to read the target file, even if the line it was on would never be executed at all. Conditional statements do not affect require(). However, if the line where require() is located is not executed, the code in the target file will not be executed. Likewise, the loop structure does not affect the behavior of require(). Although the code contained in the object file is still the body of the loop, require() itself only runs once.


Note: Since this is a language construct and not a function, it cannot be called by "variable functions".

include()
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() results in 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 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 within 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 16-4. Includes in functions

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 within valid php opening and closing tags.

If "url fopen wrappers" are enabled in PHP (default configuration), you can use URLs (via http or other supported wrapping protocols - see appendix l for supported protocols) instead of local files to specify what is to be included document. 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 is actually run on the remote server, and the local script includes its results.

Warning
Windows versions of PHP before version 4.3.0 do not support remote file access with this function, even if the allow_url_fopen option is activated.

Example 16-5. include() via http

/* this example assumes that www.zhutiai.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 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 statement groups (in curly braces) when used in conditional statements.

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

// 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 the include call can be obtained like a normal function.

Note: In PHP 3, return cannot appear in included files unless called within a function. In this case return() acts on the function rather than 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 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" a file into a variable are to use fopen(), file() or include() in conjunction with an output control function.

Note: Since this is a language structure rather than a function, it cannot be called by "variable functions".

require_once()
The require_once() statement includes and runs the specified file during script execution. This behavior is similar to the require() statement, the only difference is that if the code in the file is already included, it will not be included again. See the documentation for require() on how this statement works.

require_once() should be used when the same file may be included more than once during script execution, and you want to ensure that it is only included once to avoid problems such as function redefinition, variable reassignment, etc.

For examples of using require_once() and include_once(), see the pear code in the latest php source program distribution package.

Note: require_once() is new in php 4.0.1pl2.

Note: Be aware that require_once() and include_once() may not behave as you expect in case-insensitive operating systems (such as windows). Example 16-8. require_once() is not case sensitive in windows

require_once("a.php"); // this will include a.php
require_once("a.php"); // this will include a.php again on windows!
?>


Warning
Windows versions of PHP before version 4.3.0 do not support remote file access with this function, even if the allow_url_fopen option is activated.

include_once()

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

include_once() should be used when the same file may be included more than once during script execution, and you want to ensure that it is only included once to avoid problems such as function redefinition, variable reassignment, etc.

For more examples of using require_once() and include_once(), see the pear code in the latest php source program distribution package.

Note: include_once() is newly added in 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 not case sensitive in windows

include_once("a.php"); // this will include a.php
include_once("a.php"); // this will include a.php again on windows!
?>


Warning
Windows versions of PHP before version 4.3.0 do not support remote file access with this function, even if the allow_url_fopen option is activated.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632301.htmlTechArticlerequire() The require() statement includes and runs the specified file. The require() statement includes and runs the specified file. See the documentation for include() for details on how includes work. require() and...
Statement:
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