Include external php files

I use some php code, such as a database connection, which is common to all pages, so I created a php file containing php code and then included this file in my HTML code, So I want to know how best include php file, the best alternative for include function. my sample code is here

<?php include "check_timeout.php"; include "connect_to_db.php"; ?> <html> <head> <title>Example</title> </head> <body> <?php mysql_close($con); ?> </body> </html> 

Thanks in advance.

+6
source share
3 answers

You have 4 choices.

include 'yourfile.php'; include_once 'yourfile.php';
require 'yourfile.php';
require_once 'yourfile.php';

Of course, you can also use "instead."

they will all do the same, except for minor differences.

if yourfile.php does not exist, the included ones will ignore this fact, and your php page will move on - without any fatal errors.

require that they, on the other hand, create a fatal error.

if you know that this file exists, it does not matter which one you select.

As for options with postfix _once, they are usually slower compared to their missing postfixed counterparts. The reason when you use include_once or require_once, PHP will do extra work to make sure that these files are really ONCE included - protecting you from a possible double situation includes if you carelessly encode and many files use many of them, and you may encounter a situation when the same file is included twice. Well, the _once option will prevent this. And this check will come with some processing cost.

I also noticed that you used β€œunlike” for your file delimiters. There is no reason to choose "over" if you will not refer to the variable names in your files, for example

$ thefile = 'yourfile.php; include "$ thefile";

and which ones to choose from these 4? it all depends on if you think that you need to forcibly solve the _once problem, then you choose include_once or require_once, and if you think that this is not necessary, then you go with the inclusion or requirement. As for include vs require, it all comes down to the fact that you want your PHP script to die or move on if your file is unavailable for some reason.

If you are interested in learning about some speed tests, here is a link for you to check. http://php.net/manual/en/function.require-once.php

I also found this on this subject.

Understanding the Difference Between Requirement and Inclusion According to the PHP manual, requiring and including "are the same in every way, except how they handle failure." However, further reading of the manual offers another very subtle difference that affects performance. when you use the require keyword, the named file is read, parsed, and compiled, when the file using the require keyword is compiled. When a file containing the include keyword is compiled, the named file does not first read, parse, and compile. Only when this line of code is a file read, parsed, and compiled. Use only require keyword if you know that you will always need this named file in the current script. If you can use its functions, use include instead. PHP opens all the files that are required, but only included files are opened as needed. In addition, you should also consider using require_once and include_once instead of require and include, respectively. In practice, it is more likely that you really want the functionality provided by the require_once and include_once functions, although the requirement is used much more often and include keywords accordingly. See the following pages in the PHP manual for more information: include, include_once

Source: http://www.stevengould.org/portfolio/developerWorks/efficientPHP/wa-effphp/wa-effphp-a4.pdf

+15
source
 require_once() 
Operator

can be used to include a php file in another, when you may need to include the called file more than once. If it is detected that the file is already included, the script call will ignore further inclusions.

If a.php is a php script calling b.php with

 require_once() 

and does not detect b.php, a.php stops, causing a fatal error.

Syntax

require_once ('name of the calling file with the path'); Example: PHP require_once () view transparency?

 <?php echo "today is:".date("Ymd"); ?> 

The above file is x.php

The above x.php file is included twice with the require_once () requirement in the next y.php file. But from the output you get that the second instance of the inclusion is ignored, because the require_once () expression ignores all such inclusions after the first. view transparency?

 <?php require_once('x.php'); require_once('x.php'); ?> 

Check out this php require_once () example in a browser

If the calling script does not find the called script's require_once statement, it stops the execution of the script call.

For performance, you can answer this

hope this helps someone!

+8
source

Always use require or require_once since you will get E_COMPILE_ERROR in case something is configured incorrectly.

In terms of performance, include and require will be functionally identical.

require itself will be as efficient as the code contained in each file.

+6
source

All Articles