Reputation: 9992
I know the difference between include
and require
by reading at following link;
Require VS Include
But if there are PHP files;
dbconenction.php
is include in it to fetch some information from database.dbconenction.php
include or require because in some functions there are queries)dbconenction.php
is include or require in it)dbconenction.php
and class.phpmailer.php
is include or require in it)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;
dbconenction.php
already exist in other PHP included or required files
do i really need to include or require it in process.php
?class.phpmailer.php
in email-functions.php
then which file comes first in process.php
? class.phpmailer.php
Or email-functions.php
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.
Upvotes: 2
Views: 1121
Reputation: 1269
You can have an init.php that have all includes that you need.
Read more: MVC
Upvotes: 0
Reputation: 1772
Don't do any of these things, use an auto-loader http://php.net/manual/en/language.oop5.autoload.php.
Or even better, use a proper framework like Symfony http://symfony.com. I haven't had to think about such things for a long time. Keep up with the latest greatest techniques and save yourself the hassle of dealing with this sort of tediousness.
Read this http://webdeveloper.gdemolished.com/stop-building-shitty-php-web-applications/
Upvotes: 0
Reputation: 3158
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 -
Good luck!~
Upvotes: 3