Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include external php files

Tags:

html

php

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.

like image 754
Pramod Avatar asked Dec 29 '12 06:12

Pramod


People also ask

Can I include PHP file from another server?

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script.

How do I include a PHP file in an HTML file?

We can insert any PHP file into the HTML code by using two keywords that are 'Include' and 'Require'. PHP include() function: This function is used to copy all the contents of a file called within the function, text wise into a file from which it is called. This happens before the server executes the code.

Can I include PHP in HTML?

As you can see, you can use any HTML you want without doing anything special or extra in your PHP file, as long as it's outside and separate from the PHP tags. In other words, if you want to insert PHP code into an HTML file, just write the PHP anywhere you want (so long as they're inside the PHP tags).


1 Answers

You have 4 options to choose from.

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

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; include "$thefile";

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. http://php.net/manual/en/function.require-once.php

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

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

like image 125
Average Joe Avatar answered Sep 22 '22 10:09

Average Joe