include function in PHP

1, include and require difference

When there is no file to be included, include generating a warning (Warning), the statement behind the program continues; and require results in a Fatal Error (Fatal error), this program is terminated.

2, the path issues (and include the require)

+c.php
+folder1
+----a.php
+----folder2
+--------b.php

a.php
    include 'folder2/b.php';
b.php
    include 'c.php';

A.php execution process

  1. It contains "b.php", and execute "b.php"
  2. In the folder1 (the current working directory, under the directory "a.php") to find "c.php", the next step is to find less than
  3. In folder2 (the current directory, under the directory "b.php") to find "c.php" under.

Whether it is "a.php", or "b.php", if the include path contains "../", then all (folder1, "a.php" directory) in the parent directory to find the current working directory

3, variable scope

  1. When a file is included, the code contains inherits "include" variable range of the row, the row available at any of the variables are available in the file it contained. The main target in the function include.
  2. All functions and classes defined in the included file have the global scope, and different variables. Variable normal variable role in inherited "include" variable range of the row, for example, in the function include.

4、include_path

set_include_path(       
    get_include_path()
    . PATH_SEPARATOR . './library' 
    . PATH_SEPARATOR . './application/models'
); 

The default include_path = ".; C: \ xampp \ php \ PEAR", after use set_include_path settings (regardless of how the file that contains the hierarchy) follow-up of all programs will use this value, the function can be called multiple times.

Reproduced in: https: //www.cnblogs.com/rainman/archive/2011/12/29/2305677.html

Guess you like

Origin blog.csdn.net/weixin_34008805/article/details/93561302