PHP Basics (3) (file load include)

Source: Internet
Author: User
Tags php basics

File loading

Review:

There are 4 file-loaded syntax forms (note, not function):

Include, include_once, require, require_once;

Their nature is the same, both for loading / importing / including / loading an external file into the current PHP code.

They only have a slight difference in 2 ways:

1, if the load file fails, there are different processing rules;include and require are different

2, if the load file duplication, there are different processing rules;XXX and xxx_once are different;

3, they are both grammatical structures, not functions, which can be used in two ways:

3.1 Include ' file path ';

3.2 Include (' file path ');

File path Issues

Regardless of which loading syntax structure, it involves file path issues. There are 3 main Types of cases:

1, relative path:

is to "./", or ". /", the beginning of the path.

./ indicates where the current Web page file is located (folder, directory);

.. / indicates the top-level location (folder, directory) where the current Web page file is located

This relative position is valid for all content in a site (including PHP,html, images,css,js files).

2 , absolute path:

There are 2 types of absolute paths:

Local absolute path:

Window system: c:/d1/d2/page3.php

UNIX Series System: /d1/d2/page3.php

Network absolute path: http://www.abc.com/d1/d2/page4.php

3 , only the file name (no path)

In the php include syntax (as in the other 3 ), if only the file name is given, and the path is not given, then there is a rule in its interior to find the file, as follows:

1, first look for the system settings in the include directory:

In the php.ini configuration file, there are:include_path settings:

visible default no setting;

This setting can actually set up multiple directories, separated by semicolons, and the system will find the files to be loaded sequentially.

Modify the include_path in php.ini to:

We can also set it in the script:

We can also use another function to get the value of the current include_path:

2, not found above, in the directory of the currently executing Web page file (so-called current working directory) find:

The so-called current Web page file is the file name displayed in the Address bar.

3, not found above, look in the same directory as the file where the current include command is located:

If a file (a) contains file b, and there is an include command in file B that contains file C,

This can happen in the case of a path problem in B at this point.

File loading in the execution process

    • Exit PHP script mode from the Include statement (enter HTML code mode)
    • Load the code in the file set by the include statement and execute it (as in the current file)
    • Exit HTML mode to re-enter PHP script mode, continue after the code

Example:

In 4include_yuanli.php:

in page3.php:

The code in 4include_yuanli.php is equivalent to:

Several differences:

    • The difference between include_once and include: the former guarantees that it will not be loaded repeatedly
    • The difference between require and include:
      • If the included file does not exist, that is, if the reference fails (an error), include warns and resumes execution of subsequent code, require directly terminates.

    • The difference between require_once and require: the former can guarantee that it will not be loaded repeatedly

return Key Words

Previously, we only had the return keyword in the function, which ended the function and could return the data;

return; // end simply;

return $x; // end, and return data $x;

So

The return keyword in the included (loaded) file also has the same effect!

Results:

Take a look at the return returned value in the form:

The result is:

PHP Basics (3) (file load include)

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.