All In One Script



PHP,HTLM,CSS,Jquery,AJAX,Javascript and etc doubts and sample codes

  • Home
  • Javascript
  • PHP
  • CSS
  • SQL/MYSQL

Include External PHP files

by Blogger 4:21:00 AM Include External PHP files PHP PHP files

Include External PHP files

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.

Slolution:

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



http://stackoverflow.com/questions/14079459/include-external-php-files

READ MORE
SHARE :

Search This Blog

Followers

  • Popular
  • Recent
  • Comments
    How to get Real IP,ISP,Country,City and etc from Visitor using PHP
    How to efficiently iterate over each Entry in a Map?
    In Java, difference between default, public, protected, and private
    How to check if PHP array is associative or sequential?
    Length of a JavaScript object
    How to return the date part only from a SQL Server datetime datatype
    Solved : curl_init() function not working in Ubuntu
    What is the difference between “INNER JOIN” and “OUTER JOIN”?
    Does finally always execute in Java?
    Get first key in a (possibly) associative array?

Instagram

About

Popular Posts

  • How to get Real IP,ISP,Country,City and etc from Visitor using PHP
    How to get Real IP,ISP,Country,City and etc from Visitor using PHP Php Get Real visiter's IP and ISP and Country and City and Countr...
  • How to efficiently iterate over each Entry in a Map?
    How to efficiently iterate over each Entry in a Map? If I have an object implementing the  Map  interface in Java and I wish to iterate...
  • In Java, difference between default, public, protected, and private
    In Java, difference between default, public, protected, and private In Java , are there clear rules on when to use each of access modifi...
  • How to check if PHP array is associative or sequential?
    How to check if PHP array is associative or sequential? PHP treats all arrays as associative, so there aren't any built in function...
  • Length of a JavaScript object
    Length of a JavaScript object If I have a JavaScript object, say var myObject = new Object (); myObject [ "firstname" ] ...
  • How to return the date part only from a SQL Server datetime datatype
    How to return the date part only from a SQL Server datetime datatype SELECT GETDATE () Returns:  2008-09-22 15:24:13.790 I want tha...
  • Solved : curl_init() function not working in Ubuntu
    Solved : curl_init() function not working in Ubuntu  Here solved the error  Fatal error: Call to undefined function curl_init() ...
  • What is the difference between “INNER JOIN” and “OUTER JOIN”?
    What is the difference between “INNER JOIN” and “OUTER JOIN”? Also how do  LEFT JOIN ,  RIGHT JOIN  and  FULL JOIN  fit in? Answ...
  • Does finally always execute in Java?
    Does finally always execute in Java? I have a try/catch block with  return s inside it. Will the finally block be called? For example...
  • Get first key in a (possibly) associative array?
    Get first key in a (possibly) associative array? What's the best way to determine the first key in a possibly associative array? My...

statcounter



statcounter



Template Created By ThemeXpose & Blogger Templates