Php file call and several functions including include_once () require_once () include () requir

Source: Internet
Author: User
The require () statement includes and runs the specified file. for details about how to work, see the include () document. Require () and include () except for how to handle failures

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

  1. Require 'prepend. php ';
  2. Require $ somefile;
  3. Require ('somefile.txt ');
  4. ?>

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.

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 () exampleVars. php

  1. $ Color = 'green ';
  2. $ Fruit = 'apple ';
  3. ?>
  4. Test. php
  5. Echo "a $ color $ fruit"; //
  6. Include 'vars. php ';
  7. Echo "a $ color $ fruit"; // a green apple
  8. ?>

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

  1. Function foo ()
  2. {
  3. Global $ color;
  4. Include 'vars. php ';
  5. Echo "a $ color $ fruit ";
  6. }
  7. /* Vars. php is in the scope of foo () so *
  8. * $ Fruit is not available outside of this *
  9. * Scope. $ color is because we declared it *
  10. * As global .*/
  11. Foo (); // a green apple
  12. Echo "a $ color $ fruit"; // a green
  13. ?>

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 ()

  1. Include 'File. php? Foo = 1 & bar = 2 ';
  2. // Works.
  3. Include 'http: // www.example.com/file.php? Foo = 1 & bar = 2 ';
  4. $ Foo = 1;
  5. $ Bar = 2;
  6. Include 'file.txt '; // works.
  7. Include 'File. php'; // works.
  8. ?>

For more information, see use remote files, fopen () and file (). Because include () and require () are special language structures, in a condition statement, you must place it in the statement group (in curly brackets ).

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

  1. // This is wrong and will not work as desired.
  2. If ($ condition)
  3. Include $ file;
  4. Else
  5. Include $ other;
  6.  
  7. // This is correct.
  8. If ($ condition ){
  9. Include $ file;
  10. } Else {
  11. Include $ other;
  12. }
  13. ?>

Processing 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 a function. In this case, return () acts on this function rather than the entire file.

Example 16-7. include () and return () statementsReturn. php

  1. $ Var = 'php ';
  2. Return $ var;
  3. ?>

Noreturn. php

  1. $ Var = 'php ';
  2. ?>

Testreturns. php

  1. $ Foo = include 'return. php ';
  2. Echo $ foo; // prints 'php'
  3. $ Bar = include 'noreturn. php ';
  4. Echo $ bar; // prints 1
  5. ?>

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.

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 that require_once () and include_once () may not be as expected in case-insensitive operating systems (such as windows. Example 16-8. require_once () is case insensitive in windows

  1. Require_once ("a. php"); // this will include a. php
  2. Require_once ("a. php"); // this will include a. php again on windows!
  3. ?>

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.

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 that include_once () and require_once () may not be as expected in case-insensitive operating systems (such as windows.

Example 16-9: include_once () in windows is case insensitive:

  1. Include_once ("a. php"); // this will include a. php
  2. Include_once ("a. php"); // this will include a. php again on windows!
  3. ?>

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.