Shehary Shehary - 5 months ago 16
PHP Question

PHP include Or require? and which file comes first and which right after?

I know the difference between

include
and
require
by reading at following link;

Require VS Include


But if there are PHP files;


  1. dbconenction.php (which has all the database related code)

  2. index.php (which has form and
    dbconenction.php
    is include in it to fetch some information from database.

  3. funtions.php (which has all the custom functions which also has the
    dbconenction.php
    include or require because in some functions there are queries)

  4. site.php (which has all the site setting fetching from database and
    dbconenction.php
    is include or require in it)

  5. config.php (a file which has some manually added variables)

  6. email-functions.php (a file which has email templates and send email functions and
    dbconenction.php
    and
    class.phpmailer.php
    is include or require in it)

  7. class.phpmailer.php

  8. process.php (where i process the form, variables, functions and send email)



So in
process.php
i have to include or require all files like
dbconenction.php
,
funtions.php
,
config.php
,
email-functions
and
class.phpmailer.php
.

My Questions;


  1. If
    dbconenction.php
    already exist in other
    PHP included or required files
    do i really need to include or require it in
    process.php
    ?

  2. If I don't include
    class.phpmailer.php
    in
    email-functions.php
    then which file comes first in
    process.php
    ?
    class.phpmailer.php
    Or
    email-functions.php

  3. And If I dont include or require
    dbconenction.php
    in any of above file beside
    index.php
    and
    process.php
    then which files comes first and which next to it incase if one PHP file variable is dependent on other PHP file;



Reason I'm asking because I develop multiple sites in php with total custom solutions but this PHP file structure always bother me and in somecases one site PHP file structure just won't work in other site at all, sometime even no error just stuck me on blank page.

Answer

As a general rule-of-thumb, I almost always use require - include will allow that blank page without errors that you talk about (Speaking of which, ini_set("display_errors", "On"); error_reporting(-1); should help you with that, in the future)

But to further elaborate on your questions:

1). With dbconnection.php - you would be better off using require_once - it sounds like (since you develop multiple websites with different needs, etc) - that using require_once for this would be most suitable - since that way you don't have to worry about where you require it - it will only get loaded once, even if you require it multiple times - Granted, you should probably go with the init.php route - where that one file does all your configurations and includes - which will keep your code cleaner over time...

2). It depends! if class.phpmailer.php does nothing but define a class - and if email-functions.php only defines functions, then it really doesn't matter which one gets included first, so long as they are both included by the time an email-function gets called - Otherwise, if email-function.php immediately does work with class.phpmailer.php - then you'll need to include that class, first -

3). I CANNOT STRESS THIS ENOUGH: YOU SHOULD NOT PASS VARIABLES BETWEEN PHP FILES - YOU SHOULD AVOID GLOBAL VARIABLES - But, if you're using require_once or include_once - then you're mostly safe...

Definitely look into MVC and MVC frameworks - and learn how to program Object-Oriented - - - it will save you a lot of time, hassle, hear-tearing and heartache over time in maintaining your projects - not to mention it'll help you not have to worry about these questions you've asked, here -

There's a pretty minimal one called 'php-login' - or, looks like it's called "Huge" now - that can get you off to a pretty simple start on how to do these sorts of things more methodically/structured/organized -

http://www.php-login.net/

Good luck!~

Comments