Set_include_path and Get_include_path usage explanation

Source: Internet
Author: User
Article reprinted from: http://blog.sina.com.cn/s/blog_4ce89f200100twbl.html

First Set_include_path This function, is in the script dynamically to php.ini in the include_path to modify.
And this include_path, it can be defined for the following include and require path range, or is predefined.

It's like:
If we do not set this value, we may need to write some full paths:

include ("123/test1.php");
include ("123/test2.php");
include ("123/test3.php");
Require ( "123/test4.php");
require ("123/test5.php");
?>
to introduce a lot of external files, but if we set the Set_include_path ("123/"), we can use the following code instead.

set_ Include_path ("123/");
include ("test1.php");
Include ( "test2.php");
include ("test3.php");
require ("test4.php");
require ("test5.php");
?>
because, when performing an include or require operation, it will go to include_path the specified path to find the file to be introduced, although I do not know this will not be optimized for performance, but it is certain that you can save some of the code. hehe ~
Well, at first , I thought it was the same thing. It's no different because I only included a file in this folder.
later, finally found the mystery of it! The hateful thing is, there is no article like me on the internet .... Is the same article around.
so this function can not only define a folder, we can define a lot of folders. As shown below, I'm going to write an initialization function:
Function Initialize ()
{
Set_include_path (Get_include_path (). Path_separator. " Core/");
Set_include_path (Get_include_path (). Path_separator. " app/");
Set_include_path (Get_include_path (). Path_separator. " Admin/");
Set_include_path (Get_include_path (). Path_separator. " lib/");
Set_include_path (Get_include_path (). Path_separator. " include/");
Set_include_path (Get_include_path (). Path_separator. " data/");
Set_include_path (Get_include_path (). Path_separator. " cache/");
}
So its path is:
.; c:\php5\pear;core/;app/;admin/;lib/;include/;d ata/;cache/
Hey, we found another one in front of us. C:\php5\pear, what the hell is going on here, in fact, if we don't write anything directly first output the default value of Include_path, you will find it is.; C:\php5\pear; it can be allowed to go to a random introduction file.
if a lot of folders are loaded again, we can write the file name directly!
But as I started with that question, why is the code of our company different from others ', originally, if I wrote a
set_ Include_path (DirName (__file__));
and then to introduce other folders of the file, it will be an error, said in my designated folder within this can not be found.
First, let's start out with a different method:

set_include_path (DirName (__file__));
$include _value = ini_get (' include_path ');
echo $include _value;
?>
The result is: D:\AppServ\www
If I go to the WWW to find test4.php this file, then no error
Include ( "test4.php");
But if I go to find
Include ( "test1.php");
You will get an error:
Warning: Include () [Function.include]:failed opening ' test1.php ' for inclusion (include_path= ' D:\AppServ\www ') inD:\AppServ\www\test.php on line6
And we also found.; C:\php5\pear has been replaced. So when we're using it, we need to add get_include_path () to the front if we don't introduce the file in just one folder. Path_separator.
Explain:
7>Get_include_path () is the default value for getting the current include_path
Path_separator is a constant, which is the path demarcation of the include, which is on the window; on Unix and Linux:
Finally, I would like to say that in fact we can also go through another way: the most primitive:
Ini_set (' include_path ', ' directory name ');
In addition , the two points to note are:
if the specified The file that is requested is not found in the directory, and the file in the current directory is introduced by default when there is exactly the name in the current page directory.
when multiple directories are specified as Include_path, and the required files are present in files with the same name in each of these directories, PHP chooses to use the files in the pre-ranked directory when setting include_path.


The above describes the Set_include_path and Get_include_path usage details, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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