php file contai...LOGIN

php file contains functions

In actual development, it is often necessary to put the common code in the program into a file, and the files using these codes only need to include this file. This method helps improve the reusability of code and brings great convenience to code writing and maintenance. In PHP, there are four methods: require, require_once, include, and include-once to include a file.

Let’s compare their differences:

FunctionContains failureCharacteristics
InlcudeReturn a warningThe file continues to execute downwards. Usually used for dynamic inclusion
RequireA fatal errorThe code will not continue to execute. Usually contains extremely important files, the entire code should not be executed
Include_oncereturns a warningIn addition to the original include function, it also Will do once detection. If the file has been included before, it no longer contains
Require_onceA fatal errorIn addition to the original function In addition, a once detection will be done to prevent the file from being included repeatedly

Note:
1, use _once with once less, because it will consume more resources to do detection work.
2, Extra Advanced
Include files only need to be compiled once, because each time include is included, the corresponding code will be executed again. How to reduce the process of re-parsing when include is executed again.

Let’s do a few experiments:

1, include contains the function of the function.

Create a functions.php file and write two functions in it:

<?php
//functions.php文件

function demo(){
   echo 'aaaa';
}

function test(){
   echo 'cccdddd';
}

?>

In the same directory as functions.php, I create a user.php file and add the functions.php file included. In this way, my functions can be placed specifically in functions.php. When these functions are needed, I will include them from there:

<?php

//user.php

include 'functions.php';

//可以直接调用
demo();

test();

?>

I know the function of include through the above example. Next, we compare include and require:

In the code, we first use include to include the non-existent test.php file,

<?php

//user.php

include 'functions.php';
include 'test.php';

//可以直接调用
demo();

test();

?>

and then use require to include the non-existent test.php file:

<?php

//user.php

include 'functions.php';
require 'test.php';

//可以直接调用
demo();

test();

?>

Through the comparison of the above example, we found:

If the test.php file does not exist include, a warning will be issued and the demo() and test() functions will continue to be executed.

The require will directly report an error, and the demo() and test() functions cannot continue to execute.

We know from the table: The difference between include and include_once is to detect whether it is included repeatedly. If include_once is included repeatedly, the corresponding file will no longer be included, while include does not care about this. Whether the file has been imported or not, import it again.

Let’s try the same user.php just now. We include functions.php twice, using include and include_once respectively:

<?php

//user.php

//这儿被包含了两次同样的函数定义文件哟
include 'functions.php';
include 'functions.php';

//可以直接调用
demo();

test();

?>

Change to include_once and try again:

<?php

<?php
//user.php

//这儿被包含了两次同样的函数定义文件哟
include_once 'functions.php';
include_once 'functions.php';

//可以直接调用
demo();

test();

?>

After you execute it separately, you will find that -include reported the following error when introducing functions.php twice this time:

QQ截图20161114112303.png

The prompt in the above picture is that the function demo() cannot be redefined.

We mentioned in the function definition chapter that a function cannot be defined twice, otherwise an error will be reported. Because we included functions.php twice, it was executed twice, so this error was reported.

The reason why include_once does not report an error is because it detects that functions.php has been included before and no longer includes it.

As for the functions of require and require_once, can you use your smartest little brain to deduce them? require_once has two characteristics:

1. The included file must exist, otherwise execution will stop

2. Duplicate inclusion checks will be done

QQ图片20161114112544.gif

Next Section
<?php //user.php //这儿被包含了两次同样的函数定义文件哟 include_once 'functions.php'; include_once 'functions.php'; //可以直接调用 demo(); test(); ?>
submitReset Code
ChapterCourseware