PHP: File loading-php Tutorial

Source: Internet
Author: User
PHP: File loading


Php file loading overview:


There are four File loading syntax forms (note, not a function): include, include_once, require, require_once;

They are essentially the same and are used to load (introduce/include/load) an external file to the current php code.


They have only two minor differences:

1: If File loading fails, there are different processing rules. Different from require, include

2: If the load is repeated, there are different processing rules. XXX is different from XXX_once.

3: they are all syntax structures, rather than functions, and can be used in two forms:

Include 'file path ';

Include ('file path ');


File path:


No matter which syntax structure is loaded, the file path problem is involved. There are three main cases:

1: relative path

Is the path starting with "./", or "./".

./Indicates the location of the current webpage File (Folder/directory );

../Indicates the upper-level location of the current webpage File (Folder/directory );

This relative position is effective for all contents of a website (including php, html, images, css, and js files ).

2: absolute path

There are two absolute paths:

Absolute local path:

Window System: c:/d1/d2/index. php

Unix:/d1/d2/index. php

Absolute network path: http://www.abc.com/d1/d2/index.php

3: only file names (no path, only file names are provided, not recommended)

In the include syntax of php (the other three are also the same), if only the file name is given, but the path is not given, then the rule for finding the file internally is as follows:

First, search in the include directory set by the system.

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

It can be seen that there is no setting by default. In fact, you can set multiple directories separated by semicolons. The system will search for them in order.

If not found, find it in the directory where the webpage file is located (current working directory)

If not found, find it in the directory of the file where the current include Command is located.

File loading process:

Exit the php script mode from the include statement (enter the 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 and enter php script mode again.


Several differences:

Difference between include_once and include: the former can ensure that it will not be repeatedly loaded

Difference between require and include: if the included file does not exist, that is, when the reference fails (error), include warning and continue to execute the subsequent code, require will directly terminate

The difference between require_once and require: The former can ensure that it will not be repeatedly loaded.


Return keyword:

Previously, we only showed the return keyword in the function. its function is to end the function and return data;

Return; // end simply;

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

Then, the return in the included (loaded) file also plays the same role!

By default, if the include operation succeeds, 1 is returned. if the loading fails, false is returned.

However, in the contained file, you can use the return statement to return the data value and terminate the subsequent execution of the file.

The data returned by return can be further processed like the function return value (for example, assigning values to other variables)

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.