PHP file references

Source: Internet
Author: User

Set_include_path (DirName (__file__)) was discovered recently when researching the company code, so a line of code that looked up the data on the web was used to clarify the role of the method.

First, the DirName (__file__) method returns the path to the file where the current code is located.

The Set_include_path function is equivalent to the environment variables of windows. Windows can find the required files from the specified path through the path to the environment variable.

When our project needs to introduce multiple class files, function files, especially when the referenced file path is deep, our code becomes the following:

1<?PHP2 include("xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/test1.php");3 include("xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/test2.php");4 include("xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/test3.php");5 include("xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/test4.php");6 include("xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/test5.php");7?>

Does it feel like writing a lot of redundant code?

If we set the Set_include_path (" xxx/xxx/xxx/xxx/xxx/xxx/ xxx/xxx/xxx/xxx/xxx/ "), then the path in front of our code can be omitted.

 1  <? php  2  set_include_path  ("xxx/xxx/xxx/xxx/xxx /xxx/xxx/xxx/xxx/xxx/xxx/" 3  include  ("test1.php"  4  include  ("test2.php"  5  include  ("test3.php"  6  require  ("test4.php"  7  require  ("test5.php"  8 ? 

The Set_include_path function can also set multiple path variables to be used under Linux: Delimited, under windows, and delimited. Like what:

1<?PHP2 Define(' A ', ' xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/');3 Define(' B ', ' yyy/yyy/yyy/yyy/yyy/yyy/yyy/yyy/');4 Define(' C ', ' zzz/zzz/zzz/zzz/zzz/zzz/zzz/zzz/');5 6 Set_include_path(A.path_separator. B.path_separator.C);7 8 include("test1.php");9 include("test2.php");Ten include("test3.php"); One require("test4.php"); A require("test5.php"); -  -?>

Finally, there are two predefined PHP variables

Path_separator: Path delimiter, include multiple path use, under Win, when you want to include multiple paths, you need to use ";" Separated, but under Linux, ":" is used.

There are also directory_separator: directory separators, on Linux is '/' windows is ' \ '.

PHP file references

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.