PHP file call and several functions including include_once () require_once () include () require (_ PHP Tutorial

Source: Internet
Author: User
Php file call and several functions including include_once () require_once () include () require (. The require () statement includes and runs the specified file. The require () statement includes and runs the specified file. For details about how to work, see the include () document. Require () and require ()
The require () statement includes and runs the specified file.

The require () statement includes and runs the specified file. For details about how to work, see the include () document.

Require () and include () are identical in all aspects except how to handle failures. Include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing the page when the file is lost, do not hesitate to use require. This is not the case with include (). The script will continue to run. Make sure that the appropriate include_path is set.

Example 16-2. basic require () example

Require 'prepend. php ';

Require $ somefile;

Require ('somefile.txt ');

?>


For more examples, see the include () document.


Note: Before php 4.0.2, the following rules apply: require () always tries to read the target file, even if its row is not executed at all. The condition statement does not affect require (). However, if the line where require () is located is not executed, the code in the target file will not be executed. Similarly, the loop structure does not affect require () behavior. Although the code contained in the target file is still the subject of a loop, require () itself runs only once.


Note: Because this is a language structure rather than a function, it cannot be called by the variable function.

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

The following documents also apply to require (). These two structures are identical except for how to handle failures. Include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing the page when a file is lost, use require (). This is not the case with include (). The script will continue to run. Make sure that the appropriate include_path is set.

When a file is included, the code contained in the file inherits the variable range of the row where the include is located. From this point on, any variables available to the calling file in this row 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"; //

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 they are defined inside the function. So it will follow the variable range of the function.

Example 16-4. functions include

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 syntax parser disconnects from the php mode at the beginning of the target file and enters the html mode to restore it at the end of the file. For this reason, any code that should be executed as php code in the target file must be included in the valid php start and end tags.

If "url fopen wrappers" is activated in php (default configuration), you can use url (via http or other supported encapsulation protocols-supported protocols are shown in Appendix l) instead of a local file to specify the file to be included. If the target server interprets the target file as php code, you can use the url request string applicable to http get to pass variables to the included file. 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 a remote server, while the local script includes the result.


Warning
Windows php versions earlier than version 4.3.0 do not support remote file access to this function, even if the allow_url_fopen option is activated.

Example 16-5. include () through 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 specified Ded 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
// 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 more information, see use remote files, fopen () and file ().

Because include () and require () are special language structures, they must be placed in the statement group (in curly brackets) in condition statements ).

Example 16-6. include () and condition 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;
}

?>


Processing the 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 calls it. You can also return values from included files. The return value of the include call can be obtained just like that of a common function.


Note: in php 3, return cannot appear in included files unless called in functions. In this case, return () acts on this 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 the include operation is successful. Note the differences in the above example. The first one uses return () in the included file but the other does not. The other methods to "include" files to variables are to use fopen (), file (), or include () together with the output control function.

Note: Because this is a language structure rather than a function, it cannot be called by the 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. The only difference is that if the code in the file has been included, it will not be included again. For how this statement works, see The require () documentation.

Require_once () should be used when the same file may be included more than once during script execution. you want to ensure that it is included only once to avoid function redefinition, variable re-assignment and other issues.

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


Note: require_once () is newly added in php 4.0.1pl2.

Note: the behaviors of require_once () and include_once () in case-insensitive operating systems (such as windows) may not be what you expected. Example 16-8. require_once () is case insensitive 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 php versions earlier than version 4.3.0 do not support remote file access to 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 has been included, it will not be included again. As the statement name 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. you want to ensure that it is included only once to avoid function redefinition, variable re-assignment and other issues.

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


Note: include_once () is newly added in php 4.0.1pl2.

Note: the behavior of include_once () and require_once () in case-insensitive operating systems (such as windows) may not be what you expected. Example 16-9. include_once () is case insensitive 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 php versions earlier than version 4.3.0 do not support remote file access to this function, even if the allow_url_fopen option is activated.

Http://www.bkjia.com/PHPjc/632301.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632301.htmlTechArticlerequire () require () statements include and run the specified file. The require () statement includes and runs the specified file. For details about how to work, see the include () document. 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.