PHP 4 contains file method differences _php Tutorial

Source: Internet
Author: User
Tags flock parse error php define
There are many similarities and differences between require () and include (). It is important to understand their differences, otherwise it is easy to make mistakes.

Require () statement

The Require () statement is used for the specified file instead of the statement itself, just like the include () statement in the C language. If the URL fopen wrappers in PHP configuration file php.ini is open (open by default), you can use the URL to specify the location of the file to make a call to the remote file.

One thing is to pay special attention when using the require () and include () statements. That is, in the included file, the processor interprets the content in HTML mode, and then reverts to PHP mode after processing the contained content. So if you need to use PHP syntax in the included file, use the correct PHP start and end tags to include the statements.

Require () and include () are just a language feature in PHP, not a function. They have many different places from functions.

For example: Require () contains a file that cannot contain a control structure and cannot use a statement such as return. Using the return statement in the files contained in require () will result in processing errors.

Unlike the include () statement, the Require () statement unconditionally reads the contents of the files it contains, regardless of whether the statements are executed. So if you want to include different files according to different conditions, you must use the Include () statement. Of course, if the statement at the location of require () is not executed, the statements in the file contained in require () will not be executed.

Require () cannot contain different files in the loop body depending on the condition. The Require () statement will only invoke the contents of the file it contains in the first execution and replace the statement itself, and when executed again, it can only execute the statement that was contained for the first time. However, the include () statement can contain different files in the loop body.

The variable in the Require () statement inherits the scope of the variable where the require () statement is located. All variables that can be accessed at the location of the require () statement can be accessed in the file contained in the Require () statement. If the require () statement is inside a function, the statements within the contained file are equivalent to those defined inside the function.

The Require () statement reads a file that uses require references before the PHP program executes, so require is usually placed at the beginning of the program. So pay special attention to the fact that the require statement has a bit of a strong, regardless of whether the program really needs to refer to the file, as long as you use the Require statement, it will include them in! Even if you are using this function for inclusion in conditional control statements, if that condition is not true, the reference file will be included! Zombies are formed, and these zombies do not play any visible role during the run, but it is obvious that it will add to the burden, so pay special attention to this! If an error is included with the Require statement, the program outputs an error message and stops running!!

If the Require () statement contains a remote file by declaring the URL of the file, and the remote server interprets the file as PHP code, the content contained in the local PHP file is processed later on the remote server. For example:

/* This example assumes that the Some_server server can interpret the. php file without interpreting the. txt file. The  variable $varfirst and $varsecond*//* are not executed correctly in the remote file, and the remote server does not process the. txt file */require ("Http://some_server/file.txt?varfirst=1 &varsecond=2 ");    /* Incorrect, this will only find the file.php file */require ("file.php?varfirst=1&varsecond=2") on the local machine;/* Correct statement */require ("Http://some_ Server/file.php?varfirst=1&varsecond=2 ");    $varfirst =1; $varsecond =2;require ("file.txt"); /* Correct statement */require ("file.php"); /* The correct statement */

Originally in php3.0, require () contains files that can use the return statement, but the condition is that the return statement cannot appear inside the {} and must appear in the global scope of the contained file. This feature of require () has been canceled in php4.0, but can still be implemented using include ().

Include () statement

The include () statement and the Require () statement have many of the same places. The function of the require () statement is fully applicable to the include () statement in the upper require () statement without explicitly stating that it cannot be applied to the include () section. Below is a description of the features and features of the include () statement that the require () statement does not have.

The include statement reads only the files to be included when it is executed. When error handling is convenient, the include statement is used, and if a containing error occurs, the program skips the include statement, although an error message is displayed but the program will continue to execute!

The PHP processor will re-process each time it encounters an include () statement, so you can use include () in conditional control statements and loop statements to contain different files, depending on the situation.

 
  

You can use the return statement to return a value in the files contained in the include () statement in php3.0 and php4.0, and stop executing the contents of the included file. But php3.0 and php4.0 are different when dealing with such situations. The return statement in php3.0 cannot be contained within {} unless it is in a function because it represents the return value of the function instead of the return value of the file. There is no such restriction in php4.0, and users can even return a number in a file, just like the return value of a function. Such statements usually report an error in php3.0. The following examples illustrate:

Assume that the contained file is test.inc and the primary file main.php is located in a directory. The contents of Test.inc are as follows:

 
  n ";    if (1)    {         return;     }    echo "After the return"
n ";? >

Assume that the following statement is included in the main.php file:

 
  n ";? >

The PHP3.0 interpreter reports an error on the second line and cannot get the return value of the include () statement. However, the following results will be obtained in the php4.0:

Before the Returnfile returned: ' 27 '

The following hypothesis main.php instead:

 
  n ";? >

The output in php4.0 is:

Before the Returnback in main.html

The output in php5.0 is also:

Before the Returnback in main.html

The output in php3.0 is:

Before the Return27back in main.html

Parse error:parse error in/apache/htdocs/phptest/main.html on line 5

The above error occurs because the return statement is inside {} and is not inside a function. If you remove {} so that it is at the outermost layer of test.inc, the output is:

Before the Return27back in main.html

27 is present because the include () return is not supported in php3.0.

Require_once () and include_once () statements

The require_once () and include_once () statements correspond to the require () and include () statements, respectively. The require_once () and include_once () statements are primarily used when you need to include multiple files, and you can effectively avoid errors where the same piece of code is included in a function or variable that is repeatedly defined. For example: If you create two files Util.inc and Fool.inc, the program code is:

Util.inc:

 
  n "; function Goodtea () {return" Olong tea tasts good! ";}? >

and Fool.inc:

 
  

These two files are then included in the error_require.php:

 
  n "; echo" required in FOOL.INCN "; echo" Running Goodtea: ". Goodtea ()."
n "; echo" Printing foo:
n "; Showvar ($foo);? >

When running error_require.php, the output is as follows:

GLOBALS is Niceglobals is nicefatal error:cannot redeclare goodtea () in Util.inc on line 4

If you use the require_once () statement instead of the require () statement, the above error will not occur. Let's change the Require () statement in error_require.php and Fool.inc to the require_once () statement and rename it to error_require_once.php, which shows the following result:

GLOBALS was Nicethis is requiring util.inc again which was alsorequired in fool.inc Running goodtea:olong tea tastes good!p Rinting Foo:array ([0] = 1 [1] = = Array ([0] = complex [1] = quaternion))

The syntax of the include_once () statement is similar to the include () statement, and the main difference is to avoid repeating the definition of a function or variable by including a single file multiple times.

The require_once statement has a reference chain that guarantees that the file will be added to your program only once, and avoids collisions between the variable value and the function name.

As with require_once statements, the Include_once statement extends the functionality of include. During the execution of the program, the specified file is included, and include_once () will not include it if the program referenced from the file was previously included. That is, you can only refer to the same file once!

The include_once () statement contains 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 contained, it will not be included again. As the name of this statement implies, it is included only once.

Include_once () should be used when the same file is likely to be included more than once during script execution, to ensure 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 ().

The return value is the same as include (). If the file is already contained, this function returns TRUE.

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

Note: Note that the behavior of include_once () and require_once () in case-insensitive operating systems (such as Windows) may not be expected.

If a file does not want to be included multiple times can be read using include_once or require_once##, the document data can be written.

 
  

http://www.bkjia.com/PHPjc/752457.html www.bkjia.com true http://www.bkjia.com/PHPjc/752457.html techarticle there are many similarities and differences between require () and include (). It is important to understand their differences, otherwise it is easy to make mistakes. The Require () statement require () statement is used for the specified file ...

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