PHP Code Reuse method

Source: Internet
Author: User
To invoke a file in a script, PHP provides several main ways. The include () and require () functions are the most common ways.
1.Require () function

When using Requier () to include an external file, as long as the PHP file is executed, the contents of the external file will be included in the PHP file, and when an error occurs in the included external file, the system will give an error and stop the execution of the PHP file.
Example:
program code to invoke file Config.inc:
<?php echo "This is what is called: Hello!";?>
Program code for current script example01.php:
<?php
echo "Example of code reuse:";
Require (' config.inc '); Call another file Config.inc, output the contents of the Config.inc
echo "is implemented using the Require function.";?>
The result is: Code Reuse Example: This is what is called: Hello! Using the Require function.
2.include () function
When using an include () to include an external file, only the PHP file is included in the Include () function, and when an error occurs in the contained external file, the system only gives a warning that the PHP file continues to execute downward.
Example:
program code to invoke file Config2.inc:
<?php echo "Include function is very simple to use!";?>
Program code for current script example02.php:
<?php
echo "Example of code Reuse 2:";
Include (' Config2.inc '); Call another file Config2.inc, output the contents of the Config2.inc
echo "<br>";
echo "is implemented using the Include function.";
?>
The result is: the use of code reuse example 2:include function, very simple! Implemented using the Include function.

You can specify a clear path by entering the full path or relative path of the file you want to include.
Example:
Include (' D: \ webpage\ mr\ sl\ 40.php ');
Include ('.. \ sl \ 40.php ');
Include (' 40.php ');
If no explicit path is specified, PHP looks for the contained file in the current directory and then looks for the directory listed in the Include_path settings in the php.ini file.
  • 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.