I am using some php code like database connection which is common for all the pages, so I created a php file which contains the php code and then I am including this file in my HTML code,
So I want to know the better way to include php file, better alternative for include function.
my example 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>
Thank you in advance.
You have 4 options to choose from.
of course you can also use ” instead of ‘.
they all will do the same thing except minor differences.
if yourfile.php does not exist, the include ones will ignore that fact and your php page will move on – without any fatal errors.
require ones on the other hand will create fatal error.
if you know that that file is there, it makes no difference as to which one you pick.
As to the options with the _once postfix, well, they tend to be slower compared to their none _once postfixed counterparts. Cause when you use the include_once or the require_once, PHP will do some extra work to make sure that those files are truly included ONCE – protecting you from a possible double include situation if you carelessly code and many files use many includes and you may run into situations where the same file gets included twice. Well, _once option will prevent that. And that checking will come with some processing cost.
I also noticed you’ve used ” as opposed to ‘ for your file delimiters. There is no reason to choose ” over ‘ unless you will be referring to variable names in your files such as
$thefile = ‘yourfile.php;
so which ones to pick out of these 4?
it all depends, if you think you do need to force the _once issue, then you pick either the include_once or require_once, and if you think that’s not needed, then you go with the include or require. As to include vs require, it all comes down to would you like your PHP script die or move on if yourfile is for some reason not accessible.
If you are curious about some speed tests, here is a link for you to check out.
I also found this on the subject matter.
Understanding the difference between require and include According to
the PHP manual, require and include “are identical in every way except
how they handle failure.” However, further reading of the manual
suggests another very subtle difference that impacts performance. When
you use the require keyword, the named file is read in, parsed, and
compiled when the file using the require keyword is compiled. When a
file containing the include keyword is compiled, the named file is not
read in, parsed, and compiled initially. Only when that line of code
is executed is the file read, parsed and compiled. Only use the
require keyword if you know you will always need that named file in
the current script. If you might use its functions, use include
instead. PHP opens up all files that are required, but only opens
included files as needed. Additionally, you should also consider using
require_once and include_once in place of require and include
respectively. In practice, it is more likely that you actually want
the functionality provided by the require_once and include_once
functions, even though it is much more common to use the require and
include keywords respectively. Refer to the following PHP manual pages
for more information: include, include_once
In terms of performance,
require are going to be functionally identical.
require itself is only going to be as performant as the code that’s contained within each file.