Differences between php include and require

Source: Internet
Author: User

 

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 12-3. Basic include () Example

 

Vars. php

<? Php

 

$ Color = 'green ';

$ Fruit = 'apple ';

 

?>

 

Test. php

<? 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 12-4. functions include

 

<? Php

 

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 (configured by default), you can use URL (HTTP) instead of local files to specify the files 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 does not currently support remote file access to this function, even if the allow_url_fopen option is activated.

 

 

Example 12-5. include () through HTTP ()

 

<? Php

 

/* This example assumes that www.example.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 12-6. include () and Condition Statement Group

 

<? Php

 

// 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 12-7. include () and return () Statements

 

Return. php

<? Php

 

$ Var = 'php ';

 

Return $ var;

 

?>

 

Noreturn. php

<? Php

 

$ Var = 'php ';

 

?>

 

Testreturns. php

<? 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.

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.