This article illustrates the usage of the Set_include_path () function in PHP. Share to everyone for your reference, specific as follows:
First look at the following code:
<?php
/** defines the root directory *
/define (' __typecho_root_dir__ ', DirName (__file__));
/** defines the plugin directory (relative path)/
define (' __typecho_plugin_dir__ ', '/usr/plugins ');
The/** setting contains the path/
@set_include_path (Get_include_path (). Path_separator.
__typecho_root_dir__. '/var '. Path_separator.
__typecho_root_dir__. __typecho_plugin_dir__);
? >
First of all:
Let's look at this global variable: __file__
That represents the full path of the file (including, of course, the file name)
That is, it has different values depending on the directory in which you are located; Of course, when it is used in the package line file, its value is the included path;
And then:
Let's look at this function:
String dirname (String path)
It's a PHP built-in function, what does it do, is to return the directory other than this file name, for example:
If your home page uses the _file_ variable:
(Assuming that your Web page is in the directory: http://localhost/web/index.php), then:
The value of the _file_ is http://localhost/web/index.php (an absolute path). And at this point dirname (_file_) is http://localhost/web/that is not index.php this file name.
and DirName (DirName (_file_)) represents the first level of the directory, and so on;
At last:
Take a look at define () This function, in fact, he is a function of defining constants, such as: Define (' MEN ', ' ooooo ');
Then you can use men to express ooooo this string;
So what is the benefit of writing, that is, when you need to modify the variable, you just have to modify it on the line, quite convenient, especially like a string such as the path!
Let's explain this code:
Define (' __typecho_root_dir__ ', DirName (__file__));
is to define __typecho_root_dir__ as the directory in which this file is located, like this definition is generally placed in the config.inc.php, then get the directory that is config.inc.php in the directory, that is, the root directory!
Define (' __typecho_plugin_dir__ ', '/usr/plugins ');
That's not to say!
As for Set_include_path (Get_include_path). Path_separator. $path); What it means, he is the path of inclusion;
For example, you have a folder: named include, which has a database connection file: conn.php ...,
You set this up:
Set_include_path ("/include")
Then you can use it directly on other pages.
Don't you see it often? It's the parameter on the string, of course you can also set multiple paths, and the middle is divided, and the sentence:
Set_include_path (Get_include_path (). Path_separator.
__typecho_root_dir__. '/var '. Path_separator.
__typecho_root_dir__. __TYPECHO_PLUGIN_DIR__);
What does that mean, for instance:
One of your pages has such a statement:
Include ('/inc/sql.php ');
Include ('/inc/conn.php ');
And you suddenly find that I put these to include files in the INC directory is not safe, how to do, to change, I would like to put in the Include directory, good, so many pages do not die tired is strange: there is no good way! Yes!!!!!!!
In the config.inc.php wrote a sentence:
Set_include_path (Get_include_path (). ' /include ')
It's so simple, yes, it's so simple! Dynamic modification!
You don't look at this: Get_include_path (). Path_separator. $path What this is, he's on a path string, in the middle. is a string concatenation symbol, which is the combination of those constants just defined, combined into a string, or that he can dynamically set the include path! If the containing path is returned correctly, false is returned incorrectly;
For more information about PHP interested readers can view the site topics: "PHP object-oriented Programming Introductory Course", "PHP Mathematical Arithmetic Skills summary", "PHP Array" Operation Techniques Encyclopedia, "PHP string (String) Usage Summary", " PHP Data structure and algorithm tutorial, "PHP Programming Algorithm Summary", "PHP Regular Expression Usage Summary", and "PHP Common database Operation Skills Summary"
I hope this article will help you with the PHP program design.